【问题标题】:Python Selenium Multiple WebdriversPython Selenium 多个 Web 驱动程序
【发布时间】:2021-05-23 06:47:56
【问题描述】:

所以我有这个 python selenium 代码,我希望它同时运行多次。所以当我激活它时,它会打开多个 webdrivers 并同时执行这个脚本。我该怎么做?

driver.get(base_url)
password_id = driver.find_element_by_id('password')
password = input("Password: ")
password_id.send_keys(password)
password_id.send_keys(Keys.ENTER)
email_id1 = EC.presence_of_element_located((By.ID, 'email'))
WebDriverWait(driver, 100).until(email_id1)
email_id = driver.find_element_by_id('email')
email_id.send_keys(user_email)
start = time.time()
print(Fore.WHITE + "STATUS:" + Fore.LIGHTYELLOW_EX + " Email Filled!")
name_id = driver.find_element_by_id('name')
name_id.send_keys(user_name)
print(Fore.WHITE + "STATUS:" + Fore.LIGHTYELLOW_EX + " Name Filled!")
button_id = driver.find_element_by_id('purchase')
end = time.time()
button_id.click()
print(Fore.WHITE + "STATUS:" + Fore.LIGHTGREEN_EX + " Processing order...")
sleep(10)
timeresult = end - start
speed = (str(timeresult))
checkout_done = driver.current_url

【问题讨论】:

    标签: python selenium webdriver


    【解决方案1】:

    您尝试过多线程吗? 下面的代码允许我一次打开两个浏览器(在单独的循环中)并在函数(first_window()、second_window())中控制它们。

    from selenium import webdriver
    from threading import Thread
    
    def first_window():
        driver = webdriver.Chrome('chromedriver.exe')
        driver.get("https://stackoverflow.com/")
    
    def second_window():
        driver = webdriver.Chrome('chromedriver.exe')
        driver.get("https://stackoverflow.com/")
    
    if __name__ == '__main__':
        Thread(target=first_window).start()
        Thread(target=second_window).start()
    

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 2023-03-13
      • 1970-01-01
      • 2020-05-04
      • 2018-01-22
      相关资源
      最近更新 更多