
与这里的答案类似,您可以启动多个Chrome浏览器线程。
execute_chrome
在这种情况下,定义一个执行Selenium代码的函数- 将所有必需的参数添加到函数定义
- 将参数作为元组传递给您的
Thread
调用,例如args=(elem, )
- 用与另一个Python包不同的名称保存脚本,例如
my_selenium_tests.py
最好从命令行而不是从交互式环境(例如Jupyter笔记本)运行脚本
from selenium import webdriver
import threading
import random
import timenumber_of_threads = 4
def execute_chrome(url):
chrome = webdriver.Chrome()
chrome.get(url)
time.sleep(1 + random.random() * 5)
driver.quit()urls = ('https://www.google.com’,
'https://www.bing.com’,
'https://www.duckduckgo.com’,
'https://www.yahoo.com’)threads = []
for i in range(number_of_threads):
t = threading.Thread(target=execute_chrome, args=(urls[i], ))
t.start()
threads.append(t)for thread in threads:
thread.join()
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)