python - python selenium 中的多线程

标签 python multithreading selenium-webdriver

我想同时打开并登录 5 个选项卡,选项卡之间没有延迟。我尝试过:

import threading
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def openurl(threadId):
    print(threading.currentThread().getName(),' Thread')
    url = ('https://www.facebook.com/')
    print(url)
    driver.execute_script("window.open('{0}')".format(url))
    #driver.title(threadId)
    time.sleep(0.1)

    driver.set_window_size(920, 680)
    driver.find_element(By.ID, "email").send_keys("xx")
    driver.find_element(By.ID, "pass").send_keys("yy")
    driver.find_element(By.ID, "loginbutton").click()


if __name__=='__main__':
    driver = webdriver.Chrome()
    windows_before  = driver.current_window_handle
    for i in range(5):
        t1 = threading.Thread(name=i,target=openurl, args=(i,))
        t1.start()
        t1.join()

但它正在抛出:

Traceback (most recent call last): File "C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\1024983\AppData\Local\Programs\Python\Python37\fb-thread.py", line 30, in openurl driver.find_element(By.ID, "email").send_keys("xx") File raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="email"]"} (Session info: chrome=78.0.3904.108)

如果我增加 sleep 时间,选项卡之间就会出现延迟。我尝试使用 driver.title 进行导航,但所有选项卡的案例标题都相同。

最佳答案

示例使用线程运行单独的浏览器,这些浏览器填充表单并在列表按钮中设置True以通知登录按钮已准备好点击。当所有浏览器在列表按钮中设置True时,所有浏览器都会单击按钮。

看起来它几乎同时运行 - 也许只有系统才能同时建立如此多的连接。

我使用列表按钮来同步所有线程。每个线程都会获得编号,以便在列表中拥有自己的位置。我不使用 append(True) 因为我不确定它是否是线程安全的。

import time
from selenium import webdriver
from threading import Thread

def func(number):
    driver = webdriver.Chrome()
    #driver.set_window_size(920, 680)
    driver.get(url)

    driver.find_element_by_id("email").send_keys("xx")
    driver.find_element_by_id("pass").send_keys("yy")
    b = driver.find_element_by_id("loginbutton")

    buttons[number] = True
    print(buttons)

    # wait for other buttons
    while not all(buttons):
        pass

    print('click', number)
    b.click()

# ---

url = 'https://www.facebook.com/'

number_of_threads = 5

#buttons = [False * number_of_threads] # create place 
buttons = []

threads = []

for number in range(number_of_threads):
    t = Thread(target=func, args=(number,)) # get number for place in list `buttons`
    t.start()
    threads.append(t)
    buttons.append(False) # create place 

for t in threads:
    t.join()
<小时/>

编辑:threading.Barrier(5)相同,5个线程必须运行barrier.wait()才能继续.

import time
from selenium import webdriver
from threading import Thread, Barrier

def func(barrier):

    driver = webdriver.Chrome()
    #driver.set_window_size(920, 680)
    driver.get(url)

    driver.find_element_by_id("email").send_keys("xx")
    driver.find_element_by_id("pass").send_keys("yy")
    b = driver.find_element_by_id("loginbutton")

    print('wait for others')
    barrier.wait()

    print('click')
    b.click()

# ---

url = 'https://www.facebook.com/'

number_of_threads = 5

barrier = Barrier(number_of_threads)

threads = []

for _ in range(number_of_threads):
    t = Thread(target=func, args=(barrier,)) 
    t.start()
    threads.append(t)

for t in threads:
    t.join()

关于python - python selenium 中的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387309/

相关文章:

Python:通过迭代加速矩阵坐标映射

python - Scrapy 不会重新下载最近下载的图像

python - 基于匹配列值压缩数据帧

python - 线程未退出python

python-3.x - 在 Python + Selenium + Firefox WebDriver 上配置代理

python - PyPI 搜索结果中的 "weight"对选择包有什么帮助?

iphone - iOS - 是否应用程序 :didFinishLaunchingWithOptions execute in main thread?

c - valgrind 卡在 thread_wrapper 中

Java Selenium 获取 JSON 响应正文

python - 虽然声明未对 Selenium Webdriver 评估为 false