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.
-
1Short 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– Abhishek Rai2020-12-19 15:35:08 +00:00Commented Dec 19, 2020 at 15:35
Add a comment
|
1 Answer
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
6 Comments
Tedd Xanthos
Is it possible to select the amount of new processes dynamically? For example maybe setting number of processes by user input?
PDHide
Yes add a for loop and do p.start() it starts number of for loop iterations
Tedd Xanthos
Could you please provide an example?
PDHide
please see the upodated answer , now 10 browsers will be opened
PDHide
Could you accept the answer by clicking the tick sign
|