[英]How to run multiple webdriver python programs at the same time?
我有4個Python腳本(3個Web驅動程序和一個主腳本)。 我想在運行mainscript.py的同時打開這3個Web驅動程序。 我使用了多處理,但你可以使用你想要的任何東西。
現在它打開bot_1.py然后是bot_2.py然后是bot_3.py 。
bot_1.py
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe")
links=['https://ro.wikipedia.org/wiki/Emil_Constantinescu','https://ro.wikipedia.org/wiki/Traian_B%C4%83sescu','https://ro.wikipedia.org/wiki/Napoleon_I']
for i in range(len(links)):
driver.get(links[i])
bot_2.py
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe")
links=['https://ro.wikipedia.org/wiki/Abraham_Lincoln','https://ro.wikipedia.org/wiki/Winston_Churchill','https://ro.wikipedia.org/wiki/Mihail_Gorbaciov']
for i in range(len(links)):
driver.get(links[i])
bot_3.py
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe")
links = ['https://ro.wikipedia.org/wiki/Gabriela_Firea', 'https://ro.wikipedia.org/wiki/Ion_Iliescu',
'https://ro.wikipedia.org/wiki/Mihai_Eminescu']
for i in range(len(links)):
driver.get(links[i])
mainscript.py
import bot_1, bot_2, bot_3
import multiprocessing
for bot in ('bot_1', 'bot_2','bot_3'):
p = multiprocessing.Process(target=lambda: __import__(bot))
p.start()
帶有xdist
擴展名的xdist
是一個選項: https : xdist
pip install pytest
pip install pytest-xdist
然后運行pytest -n NUM
,其中NUM
是要運行的進程數(或者在您的情況下 - webdriver實例)。 我現在不能完全記住,但我認為上面的命令運行當前文件夾中的所有.py
文件。
您還可以使用Behave +行為並行。
這是並行運行的。 但它可能並不明顯,因為對我來說這是兩個重疊的窗口。 所以,我添加了time.sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from multiprocessing import Process
#import time
def run(urls):
# print ("run", urls)
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--no-default-browser-check')
options.add_argument('--disable-gpu')
options.add_argument('--disable-extensions')
options.add_argument('--disable-default-apps')
options.binary_location = '/opt/chrome-linux.63.0.3239.b.508580/chrome'
driver = webdriver.Chrome(
executable_path='/opt/chromedriver/chromedriver',
options=options,
)
for url in urls:
# time.sleep(5)
driver.get(url)
# print driver.title
driver.quit()
allurls = [
['http://ya.ru', 'http://google.ru'],
['https://ro.wikipedia.org/wiki/Emil_Constantinescu',
'https://ro.wikipedia.org/wiki/Traian_B%C4%83sescu'],
]
processes = []
for urls in allurls:
p = Process(target=run, args=(urls,))
processes.append(p)
p.start()
for p in processes:
p.join()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.