Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让多个selenium浏览器同时运行?_Python_Selenium - Fatal编程技术网

Python 如何让多个selenium浏览器同时运行?

Python 如何让多个selenium浏览器同时运行?,python,selenium,Python,Selenium,我目前正在利用selenium制作一个脚本。我希望同时运行多个“任务”。如何编写代码,使同一代码一次运行多次?您可以使用线程处理。下面是一个简短的例子: from selenium import webdriver import threading def go_to_example(driver): driver.get('https://example.com') drivers = [] for _ in range(3): drivers.append(

我目前正在利用selenium制作一个脚本。我希望同时运行多个“任务”。如何编写代码,使同一代码一次运行多次?

您可以使用
线程处理
。下面是一个简短的例子:

from selenium import webdriver
import threading


def go_to_example(driver):
    driver.get('https://example.com')


drivers = []
for _ in range(3):
    drivers.append(
        webdriver.Chrome()
    )
threads = [threading.Thread(target=go_to_example, args=(d,)) for d in drivers]
for t in threads:
    t.start()
for t in threads:
    t.join()

@非常感谢分享。我已经更新了代码,使之更具python风格。我是否应该将整个程序变成一个函数,这样它就可以与线程一起工作?这取决于您正在尝试做什么。通常,封装实现以支持线程是很重要的。在这种情况下,您的selenium功能可能位于
go_to_example
函数中,它不一定是整个程序。Hmmmm。我得到这个错误:“TypeError:'WebDriver'对象不可编辑”