Selenium,在不同的循环中使用相同的驱动程序
我正在尝试自动化一个浏览器操作,两个浏览器并行运行。我可以轻松地启动驱动程序,但在启动之后,我似乎无法使用名称driver1、driver2或我为第二步构建的循环访问它们Selenium,在不同的循环中使用相同的驱动程序,selenium,for-loop,selenium-webdriver,Selenium,For Loop,Selenium Webdriver,我正在尝试自动化一个浏览器操作,两个浏览器并行运行。我可以轻松地启动驱动程序,但在启动之后,我似乎无法使用名称driver1、driver2或我为第二步构建的循环访问它们 drivers = ['driver1', 'driver2'] #First step: load the drivers for dr in drivers: dr = webdriver.Chrome(executable_path='mypathtoseleniumbrowser') #Second step:
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”和“driver2”来定义的,否则我会访问它们。
有人能帮忙吗?谢谢,非常感谢。通过
驱动程序=['driver1','driver2']
您定义了包含两个字符串的列表驱动程序
,而不是两个WebDriver对象您可以执行类似操作或使用多线程
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()
谢谢!第一种方法很好用。祝你有美好的一天!