我正在尝试自动化浏览器操作,两个浏览器并行运行。我可以轻松启动驱动程序,但之后我似乎无法使用名称 driver1、driver 2 或我为第二步构建的循环访问它们。
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”定义的,否则我可以访问它们。 任何人都可以帮忙吗?谢谢,非常感谢。
答案 0 :(得分:1)
通过 drivers = ['driver1', 'driver2']
您定义了包含两个字符串的列表 drivers
,而不是两个 WebDriver
对象
答案 1 :(得分:1)
您可以执行此类操作或使用多线程。
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()