【问题标题】:How would I have multiple selenium browsers running at the same time?我如何同时运行多个 selenium 浏览器?
【发布时间】:2020-11-08 21:32:22
【问题描述】:

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

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您可以为此使用threading。这是一个简短的例子:

    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()
    

    【讨论】:

    • @jizhihaoSAMA 感谢分享。我已将代码更新为更 Pythonic。
    • 我应该把我的整个程序变成一个函数,以便它可以与线程一起工作吗?
    • 这取决于你想要做什么。通常,封装实现以支持线程很重要。在这种情况下,您的 selenium 功能可能会放在 go_to_example 函数中,它不必是整个程序。
    • 嗯。我收到此错误:“TypeError: 'WebDriver' object is not iterable”
    猜你喜欢
    • 2013-05-09
    • 2019-03-30
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2019-11-10
    相关资源
    最近更新 更多