Python 使用selenium优化线程

Python 使用selenium优化线程,python,multithreading,selenium,tkinter,python-multithreading,Python,Multithreading,Selenium,Tkinter,Python Multithreading,我的目标: 我使用for循环来运行多个selenium脚本线程。 这些脚本都是相同的,并且遵循基本的selenium原则 单击按钮 向文本框发送文本 正在等待元素可单击或可见 以及使用time.sleep()来模仿人类行为 所有这些脚本都需要在两分钟内完成 e、 g.我在下午6:00开始循环,所有线程应在下午6:02完成 注意每个脚本需要1-1.5分钟才能完成,所以我的问题不是增加脚本时间 我的问题: 当我运行我的代码时,它工作正常,一切都很好,但我觉得它没有得到太多的优化 例如,当我的

我的目标:

我使用
for
循环来运行多个selenium脚本线程。 这些脚本都是相同的,并且遵循基本的selenium原则

  • 单击按钮
  • 向文本框发送文本
  • 正在等待元素可单击或可见
  • 以及使用
    time.sleep()
    来模仿人类行为
所有这些脚本都需要在两分钟内完成

  • e、 g.我在下午6:00开始循环,所有线程应在下午6:02完成
注意每个脚本需要1-1.5分钟才能完成,所以我的问题不是增加脚本时间

我的问题:

当我运行我的代码时,它工作正常,一切都很好,但我觉得它没有得到太多的优化

例如,当我的线程启动时,我的鼠标指针上通常有一个蓝色的加载圆圈,所以我知道线程占用了CPU

当我使用Tkinter GUI运行selenium线程时,我不希望线程导致GUI延迟或减慢

最后,我的问题是如何使用硒优化线程。


我已经为我的原始代码创建了一个最小的复制示例

import threading
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time


class go(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        options = webdriver.ChromeOptions()
        options.add_argument('--headless', )
        self.browser = webdriver.Chrome('chromedriver.exe',options=options)
        self.browser.get('https://www.wikipedia.org/')

        WebDriverWait(self.browser, 10).until(
            EC.presence_of_element_located((By.ID, "searchInput"))).send_keys('Python',Keys.ENTER)
        time.sleep(3)
        print('Moved To Next Section ')

        WebDriverWait(self.browser, 10).until(
            EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Computing"))).click()
        time.sleep(3)
        print('Moved To Next Section ')

        WebDriverWait(self.browser, 10).until(
            EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "People"))).click()
        time.sleep(3)

        print('Moved To Next Section ')
        WebDriverWait(self.browser, 10).until(
            EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Roller coasters"))).click()
        time.sleep(3)

        print('Moved To Next Section ')
        WebDriverWait(self.browser, 10).until(
            EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Vehicles"))).click()
        time.sleep(3)

        print('Moved To Next Section ')
        WebDriverWait(self.browser, 10).until(
            EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Weaponry"))).click()
        time.sleep(100)


for count in range(70):
    go().start()

您一次启动70个chrome进程,所以是的,当然希望CPU达到100%。除了摆弄chrome标志之外,这里没什么可做的。你一次启动70个chrome进程,所以是的,当然希望CPU达到100%。除了摆弄chrome标志,这里没什么可做的。