![](/img/trans.png)
[英]Python Selenium Webdriver Handling Multiple Windows At The Same Time
[英]Is it possible to be active in multiple windows at the same time in Selenium?
我正在嘗試構建一個機器人,它可以同時檢查多個站點並在有貨時購買商品。 我已經為機器人制作了腳本,但我不知道如何同時在多個站點上運行它。
有幾種方法可以實現這一點:
只需在多個終端中運行您的代碼。
簡單來說,threading 的作用是生成多個同時運行 function main()
的“線程”。 請參閱下面的簡單演示。 這樣做是為了產生 3 個線程,“針對”主要 function,每個主要 function 訪問 python.org 1000 秒:
import time
from threading import Thread
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
# your main monitoring program goes here
def main():
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("http://www.python.org")
time.sleep(1000)
driver.quit()
if __name__ == '__main__':
threads = []
for _ in range(3):
t = Thread(target=main, daemon=True)
t.start()
threads.append(t)
for thread in threads:
thread.join()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.