【问题标题】:Running code with multiple browser at the same time多个浏览器同时运行代码
【发布时间】:2021-01-11 19:28:29
【问题描述】:

这是我的代码构造,问题是,在这种情况下,代码没有在同时运行的 2 个(范围(2))不同的浏览器中运行。取而代之的是,该程序将代码加倍并仅在一个浏览器中运行。如何让代码在多个浏览器中运行?谢谢

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time, requests, cfscrape, threading
from time import gmtime, strftime
from termcolor import colored
from threading import Thread

driver = 
webdriver.Chrome(executable_path="...")

def main():

    def gotohomepage():
        ...
    gotohomepage()


    def cookies():
        ...
    cookies()


    def queuechecker():
        ...
    queuechecker()


    def retrieving_productpage1():
        ...
    retrieving_productpage1()


    def gettingProductUrl():
        ...
    gettingProductUrl() 


threads = []

for i in range(2):
    t = threading.Thread(target=main)
    threads.append(t)
   t.start()

for thread in threads:
    t.join()

main()

【问题讨论】:

  • 您可以尝试将driver = webdriver.Chrome(executable_path="...") 移动到def main():

标签: python python-3.x selenium selenium-webdriver webdriver


【解决方案1】:

发生这种情况的原因是因为您已经全局声明了 webdriver,这导致浏览器只打开一次。但是,您通过线程执行了两次代码,这就是您获得此输出的原因。

可能的解决方案:尝试将 webdriver 初始化移动到 main 中,这将导致第二次打开新的浏览器。

示例:https://www.lambdatest.com/blog/parallel-testing-in-selenium-webdriver-with-python-using-unittest/

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2011-10-27
    • 2013-12-21
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多