0

I'm a newbie when it comes to coding and I've been experimenting with python for the past few weeks. I've started working with selenium recently and from what I have found in my research online, it seems that it's possible to have multiple browser windows open at the same time (I have already done that) but you can only control one of them at a time by using their handle id. My question is: Is it possible to control multiple browsers at the same time without having to switch over to one or the other? And if so I would greatly appreciate any guidance as to what I have to look for.

1
  • 1
    Short answer is no, it cannot be done. You can have multiple browsers but they will overload the memory. Selenium is a blocking code. This means that it is not a a good fit for asynchronous jobs. Commented Dec 19, 2020 at 15:35

1 Answer 1

1
from selenium import webdriver

import time

from multiprocessing import Process

def f():
    driver = webdriver.Chrome(r"C:\Users\prave\Downloads\travelBA\chromedriver.exe")
    driver.get(
    "https://stackoverflow.com/questions/9943771/adding-a-favicon-to-a-static-html-page")
    head = driver.find_element_by_tag_name("head")

    link = driver.find_element_by_css_selector('link[rel="shortcut icon"]')

    b = driver.execute_script('''var link = document.createElement("link");

    link.setAttribute("rel", "icon");
    link.setAttribute("type", "image/png");
    link.setAttribute(
    "href", "https://i.sstatic.net/uOtHF.png?s=64&g=1");

    arguments[1].remove();
    arguments[0].appendChild(link);
    return "hiuiiiii"
    ''', head, link)

    print(b)
    time.sleep(5)
p1=[]
if __name__ == '__main__':
    for x in range(10):
        p1.append(Process(target=f))
        p1[-1].start()

    for x in p1:
        x.join()

you can use multi-process see the example above

Sign up to request clarification or add additional context in comments.

6 Comments

Is it possible to select the amount of new processes dynamically? For example maybe setting number of processes by user input?
Yes add a for loop and do p.start() it starts number of for loop iterations
Could you please provide an example?
please see the upodated answer , now 10 browsers will be opened
Could you accept the answer by clicking the tick sign
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.