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
    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. – Abhishek Rai Dec 19 '20 at 15:35

1 Answers1

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.stack.imgur.com/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

PDHide
  • 18,113
  • 2
  • 31
  • 46