简体   繁体   English

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

[英]How to run multiple webdriver python programs at the same time?

I have 4 Python scripts (3 web drivers and a main script). 我有4个Python脚本(3个Web驱动程序和一个主脚本)。 I would like to open these 3 web drivers at the same time when I run the mainscript.py . 我想在运行mainscript.py的同时打开这3个Web驱动程序。 I used multiprocessing but you can use whatever you want. 我使用了多处理,但你可以使用你想要的任何东西。

Now it opens bot_1.py and then bot_2.py and then bot_3.py . 现在它打开bot_1.py然后是bot_2.py然后是bot_3.py

bot_1.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 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 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 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()

PyTest with the xdist extension is one option: https://docs.pytest.org/en/3.0.0/xdist.html 带有xdist扩展名的xdist是一个选项: httpsxdist

pip install pytest
pip install pytest-xdist

Then you run pytest -n NUM , where NUM is the number of processes (or in your case - webdriver instances) you want to run. 然后运行pytest -n NUM ,其中NUM是要运行的进程数(或者在您的情况下 - webdriver实例)。 I can't exactly remember now, but I think the command above runs all .py files in your current folder. 我现在不能完全记住,但我认为上面的命令运行当前文件夹中的所有.py文件。

You can also use Behave + behave-parallel. 您还可以使用Behave +行为并行。

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

This runs in parallel. 这是并行运行的。 But it maybe not obvious, because for me it was two overlapping windows. 但它可能并不明显,因为对我来说这是两个重叠的窗口。 So, i added time.sleep 所以,我添加了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM