在SeleniumPython中同时运行两个用户



我有一个脚本,其中我在两个网页中记录两个用户,一个在正常模式下,另一个在匿名模式下。我想要的是同时运行两个web驱动程序。我不知道该用什么。我刚刚定义了两个独立的函数。我希望他们同时跑步。我应该使用多线程、多处理还是其他与Selenium相关的东西?我的代码如下:

def user_qsinav_normal(username, password):
options = webdriver.ChromeOptions()
#options.add_argument("--incognito")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path=r"C:Users**DesktopChromeDriverchromedriver.exe", options=options)
driver.get("https://demo.qsinav.com/")
driver.maximize_window()
driver.find_element_by_xpath("/html/body/nav/div/div[3]/ul/li[2]/a").click()
print("Looking for the email element!")
user_input = driver.find_element_by_id("email")
user_input.send_keys(username)
print("Looking for the password element!")
password_input = driver.find_element_by_id("password")
password_input.send_keys(password)
print("Looking for the giris element!")
giris_buton = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/form/div[2]/div[3]/button")
giris_buton.click()
print("Looking for the sinav modulü element!")
sinav_modulu = driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[4]/a")
sinav_modulu.click()
driver.implicitly_wait(5)
print("Looking for the sınavlar element!")
sinavlar = driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[4]/ul/li[1]/a")
sinavlar.click()
driver.implicitly_wait(5)
print("Looking for the sınavi baslat element!")
sinavi_baslat = driver.find_element_by_css_selector("#datatableExam > tbody > tr > td:nth-child(7) > div > button")
sinavi_baslat.click()


def user_qsinav_gizli(username, password):
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path=r"C:Users**DesktopChromeDriverchromedriver.exe", options=options)
driver.get("https://demo.qsinav.com/")
driver.maximize_window()
driver.find_element_by_xpath("/html/body/nav/div/div[3]/ul/li[2]/a").click()
print("Looking for the email element!")
user_input = driver.find_element_by_id("email")
user_input.send_keys(username)
print("Looking for the password element!")
password_input = driver.find_element_by_id("password")
password_input.send_keys(password)
print("Looking for the giris element!")
giris_buton = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/form/div[2]/div[3]/button")
giris_buton.click()
print("Looking for the sinav modulü element!")
sinav_modulu = driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[4]/a")
sinav_modulu.click()
driver.implicitly_wait(5)
print("Looking for the sınavlar element!")
sinavlar = driver.find_element_by_xpath("/html/body/div[2]/div/nav/ul/li[4]/ul/li[1]/a")
sinavlar.click()
driver.implicitly_wait(5)
print("Looking for the sınavi baslat element!")
sinavi_baslat = driver.find_element_by_css_selector("#datatableExam > tbody > tr > td:nth-child(7) > div > button")
sinavi_baslat.click()

user_qsinav_normal(USERNAME1, PASSWORD1)
user_qsinav_gizli(USERNAME2, PASSWORD2)

线程处理适用于较小的数字。

如果你正在寻找10-100个像你在评论中提到的用户(我没有足够的声誉来评论,所以在回答中回答(,在一台机器上运行它们不是一个好主意。

Selenium网格的实现原因相同1(并行运行测试,2(使用不同的浏览器和操作系统组合运行相同的测试。

所以我的建议是试试硒网格。

如果你对基于docker的解决方案感兴趣,你可以使用Zalenium或Selenium docker

尝试使用线程:

import threading
t1 = threading.Thread(target=user_qsinav_normal, args=(USERNAME1, PASSWORD1))
t2 = threading.Thread(target=user_qsinav_gizli, args=(USERNAME2, PASSWORD2))
t1.start()
t2.start()

相关内容

  • 没有找到相关文章

最新更新