同时使用多个浏览器运行代码



这是我的代码构造,问题是,在这种情况下,代码没有在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()

之所以会发生这种情况,是因为您已经全局声明了Web驱动程序,这导致浏览器只打开一次。然而,您通过线程执行了两次代码,这就是为什么您会得到这个输出。

可能的解决方案:尝试将网络驱动程序初始化移到main中,这将导致在第二次尝试中打开一个新的浏览器。

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

最新更新