gpt4 book ai didi

python - 如何同时运行多个webdriver python程序?

转载 作者:行者123 更新时间:2023-11-28 22:20:15 24 4
gpt4 key购买 nike

我有 4 个 Python 脚本(3 个网络驱动程序和一个主脚本)。我想在运行 mainscript.py 时同时打开这 3 个网络驱动程序。我使用了多处理,但你可以使用任何你想要的。

现在它打开 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])

主脚本.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 扩展名的 PyTest 是一种选择: https://docs.pytest.org/en/3.0.0/xdist.html

pip install pytest
pip install pytest-xdist

然后你运行 pytest -n NUM,其中 NUM 是你想要运行的进程数(或者在你的情况下 - webdriver 实例)。我现在记不清了,但我认为上面的命令会运行当前文件夹中的所有 .py 文件。

您还可以使用 Behave + behave-parallel。

https://github.com/hugeinc/behave-parallel

关于python - 如何同时运行多个webdriver python程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49109450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com