首页 > 解决方案 > 如何在 Python 中将线程用于 Selenium 的并行实例?

问题描述

我正在尝试使用 Selenium 设置一些 Web 自动化,我试图在这里解决这个问题,不幸的是有点过时了,我使用的是 python 2,我使用的是 python 3。用最简单的术语来说,我要去使用 discord 的 onMessage 事件将 url 发送到我将启动自动化的所有线程。但是,我在让这个与 Selenium 一起工作时遇到了一些问题,当将其与此处的其他答案进行比较时,我没有看到直接的区别。

main.py:23: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome('./chromedriver', chrome_options=options)
Thread-1 True
Thread-1 Received https://google.com
Fatal Python error: could not acquire lock for <_io.BufferedWriter name='<stdout>'> at interpreter shutdown, possibly due to daemon threads

Thread 0x00007f173c273700 (most recent call first):
  File "main.py", line 39 in openWebpage
  File "main.py", line 34 in run
  File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
  File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Current thread 0x00007f173d9e8740 (most recent call first):
Aborted

不管我声明了多少线程,只有一个 webdriver 打开并且不执行get(message)导航到 URL。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.support.ui import Select
from selenium.webdriver.chrome.options import Options
import threading
import time
import re
import discord
import asyncio
import json
from multiprocessing import Queue

print_lock = threading.Lock()

class MyThread(threading.Thread):

    options = Options()
    #options.add_argument('--headless')
    #options.add_argument('--disable-gpu')
    driver = webdriver.Chrome('./chromedriver', chrome_options=options)
    
    def __init__(self, queue, args=(), kwargs=None):
        threading.Thread.__init__(self, args=(), kwargs=None)
        self.queue = queue
        self.daemon = True
        self.receive_messages = args[0]

    def run(self):
        print(threading.currentThread().getName(), self.receive_messages)
        val = self.queue.get()
        self.openWebpage(val)
        
    def openWebpage(self, message):
        if self.receive_messages:
            driver.get(message)
            with print_lock:
                print(threading.currentThread().getName(), "Received {}".format(message))

def main():
    threads = []
    for t in range(5):
        q = Queue()
        threads.append(MyThread(q, args=(t % 2 == 0,)))
        threads[t].start()
        time.sleep(0.1)

    for t in threads:
        t.queue.put("https://google.com")

if __name__ == '__main__':
    main()

我究竟做错了什么?

标签: pythonseleniumpython-multithreading

解决方案


推荐阅读