Issue
I have this example of code:
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
links = ['https://en.wikipedia.org/wiki/File:Mechanitis_Polymnia_Chrysalis.jpg',
'https://en.wikipedia.org/wiki/File:William_Cornwallis_as_Admiral_(cropped).jpg',
'https://en.wikipedia.org/wiki/File:Battle_of_Saint_Charles.jpg',
'https://en.wikipedia.org/wiki/File:Stephen_Curry_Shooting_(cropped)_(cropped).jpg']
def open(url):
chromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=chromeOptions)
driver.get(url)
print('+')
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as ex:
for link in links:
ex.submit(open, link)
Everything works well, and different links are pasted into each browser, which is what I want. But each time, new browser windows are created.
How to implement the same two browser instances all the time and put different links in each?
Solution
I modified your code a little using the same driver instead of opening a new one and it's working
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
links = ['https://en.wikipedia.org/wiki/File:Mechanitis_Polymnia_Chrysalis.jpg',
'https://en.wikipedia.org/wiki/File:William_Cornwallis_as_Admiral_(cropped).jpg',
'https://en.wikipedia.org/wiki/File:Battle_of_Saint_Charles.jpg',
'https://en.wikipedia.org/wiki/File:Stephen_Curry_Shooting_(cropped)_(cropped).jpg']
def open(url, index):
drivers[index].execute_script(f"window.open('{url}','_self')")
print('+')
NB_WINDOWS = 2
chromeOptions = webdriver.ChromeOptions()
drivers = []
for i in range(NB_WINDOWS):
drivers.append(webdriver.Chrome(options=chromeOptions))
print("Drivers : ", len(drivers))
with ThreadPoolExecutor(max_workers=2) as ex:
i = 0
for link in links:
ex.submit(open, link, i % NB_WINDOWS)
i = i+1
Answered By - Nabil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.