栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Selenium为array(list)中的每个项目创建多个chrome线程并同时执行功能

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Selenium为array(list)中的每个项目创建多个chrome线程并同时执行功能

与这里的答案类似,您可以启动多个Chrome浏览器线程。

  • execute_chrome
    在这种情况下,定义一个执行Selenium代码的函数
  • 将所有必需的参数添加到函数定义
  • 将参数作为元组传递给您的
    Thread
    调用,例如
    args=(elem, )
  • 用与另一个Python包不同的名称保存脚本,例如
    my_selenium_tests.py
  • 最好从命令行而不是从交互式环境(例如Jupyter笔记本)运行脚本

    from selenium import webdriver

    import threading
    import random
    import time

    number_of_threads = 4

    def execute_chrome(url):
    chrome = webdriver.Chrome()
    chrome.get(url)
    time.sleep(1 + random.random() * 5)
    driver.quit()

    urls = ('https://www.google.com’,
    'https://www.bing.com’,
    'https://www.duckduckgo.com’,
    'https://www.yahoo.com’)

    threads = []
    for i in range(number_of_threads):
    t = threading.Thread(target=execute_chrome, args=(urls[i], ))
    t.start()
    threads.append(t)

    for thread in threads:
    thread.join()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401434.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号