Selenium,在不同的循环中使用相同的驱动程序



我正在尝试使用两个并行运行的浏览器来实现浏览器操作的自动化。我可以很容易地启动驱动程序,但在那之后,我似乎无法使用名称driver1、driver2或我为第二步构建的循环来访问它们。

drivers = ['driver1', 'driver2']
#First step: load the drivers
for dr in drivers: 
dr = webdriver.Chrome(executable_path='mypathtoseleniumbrowser')
#Second step: perform different operations in loop, in both drivers, without restarting them
for dr in drivers:
dr.get('https://www.google.com')

它产生的AttributeError是:"'str对象没有属性"get">

因此,驱动程序似乎不是由名称"driver1"one_answers"driver2"定义的,否则我无法访问它们。有人能帮忙吗?谢谢,非常感谢。

您可以执行类似的操作或使用多线程。

drivers_instance = []
for i in range(2):
driver = webdriver.Chrome(executable_path='mypathtoseleniumbrowser')
driver_instance.append(driver)

多线程中的一些问题

from threading import Thread
def setUp():
driver = webdriver.Chrome(executable_path='mypathtoseleniumbrowser')

可能在主中

threads=[]
for i in range(2):
process = Thread(target=setUp)
process.start()
threads.append(process)
for process in threads:
process.join()

通过drivers = ['driver1', 'driver2'],您定义的列表drivers包含两个字符串,而不是两个WebDriver对象

最新更新