python - 如何并行运行多个 Selenium 驱动程序?

标签 python python-3.x multithreading selenium parallel-processing

如何并行运行多个 selenium 驱动程序以使用文本文件输入(电子邮件:密码格式)登录 Instagram。

我想打开n个驱动程序,并对每个驱动程序并行执行,而不是一一执行。

我尝试过线程,但它只是停留在刷新相同的 Selenium 驱动程序上(也许我做错了)。

我应该使用线程还是多重处理?

我是新手,所以老实说我不知道​​该怎么做 这是我当前在一个驱动程序中执行此操作的代码片段

executable_path = "chromedriver.exe"
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=executable_path, options=options)
file = open("list.txt", "r")
for line in file:
    pieces = line.split(":")
    myemail = pieces[0]
    mypass = pieces[1]
    driver.get('https://www.instagram.com/accounts/login/')
    time.sleep(5)
    element = driver.find_elements_by_css_selector('form input')[0]
    element.send_keys(myemail)
    element1 = driver.find_elements_by_css_selector('form input')[1]
    element1.send_keys(mypass)

最佳答案

建议使用python进行多处理,

下面是并行打开不同网址的示例

import multiprocessing as mp


def worker(url):
    executable_path = "chromedriver.exe"
    options = Options()
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(executable_path=executable_path, options=options)
    driver.get(url)
    # do the necessary operations
    # task1; task2; task3
    # close instance after completed
    driver.quit()


url_list = ["https://github.com/Aqua-4/auto-insta/blob/master/refresh_db.py","https://stackoverflow.com/questions/59706118/how-to-run-multiple-selenium-drivers-parallelly"]
if __name__ == '__main__':
    p = mp.Pool(mp.cpu_count())
    p.map(worker, url_list)

这将打开与您设备上的核心数量一样多的 selenium 实例,您也可以 mp.cpu_count() 更改此数字,但不建议将其增加到超过该数字设备上的核心数

关于python - 如何并行运行多个 Selenium 驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59706118/

相关文章:

java - Java中如何让ThreadPoolExecutor立即执行

python - 如何为当前列表组添加 `active` 类?

python - 在 ndb 对象中动态设置属性

python - 列表内具有相同名称的列表

python - 将 grid.lines 添加到 tkinter 不起作用

ruby-on-rails - 在任何源中都找不到 thread_safe-0.3.0

python - www.example.com/post/21/edit 是 RESTful URI 吗?我想我知道答案,但还有一个问题

python - django login_required 装饰器,即使在登录后也总是重定向到登录页面

python-3.x - 如何从 UCI Machine Learning Repository 将数据集(.data 和 .names)直接读入 Python DataFrame

java - Java中的两个线程不是多线程