python - 打开多线程后与 selenium 实例交互
问题描述
在产生 5 个线程后,我试图同时与不同的 selenium 实例进行交互。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
import threading
path = r"path"
torexe = os.popen(r"path")
profile = FirefoxProfile(r"path")
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
options = webdriver.FirefoxOptions()
options.add_argument('--incognito')
options.add_argument('--headless')
#driver = webdriver.Firefox(firefox_profile=profile,options=options,executable_path=path)
def open_thread():
driver = webdriver.Firefox(options=options, executable_path=path)
driver.get("https://time.is/") # replace here by driver.get(test_url)
time = driver.find_element_by_xpath("//*[@id='clock0_bg']").text
print(time)
driver.quit()
N = 3 # Number of browsers to spawn
thread_list = list()
while True:
# Start test
for i in range(N):
t = threading.Thread(name='Test {}'.format(i), target=open_thread())
t.start()
#time.sleep(1)
print(t.name + ' started!')
thread_list.append(t)
# Wait for all threads to complete
#for thread in thread_list:
现在它正在打开一个实例,然后打印时间并关闭浏览器。但我想先打开 3 个实例,然后打印 3 个时间,然后关闭浏览器,然后重新开始。
我的直觉是找出时间并在最后一个 for 循环中打印出来。但是对驱动程序的引用不起作用:
def open_thread():
driver = webdriver.Firefox(options=options, executable_path=path)
driver.get("https://time.is/") # replace here by driver.get(test_url)
N = 3 # Number of browsers to spawn
thread_list = list()
while True:
# Start test
for i in range(N):
t = threading.Thread(name='Test {}'.format(i), target=open_thread())
t.start()
#time.sleep(1)
print(t.name + ' started!')
thread_list.append(t)
# Wait for all threads to complete
for thread in thread_list:
time = driver.find_element_by_xpath("//*[@id='clock0_bg']").text
print(time)
driver.quit()
解决方案
推荐阅读
- python - 如何在后台/离线运行 Dash App
- c# - 为什么 C# 编译器生成没有闭包的匿名委托作为实例方法,而不是作为同一类型的静态方法?
- angular - 由于 import/no-deprecated 将 angular 从 12.0.2 更新到 12.2.12 后,eslint 超级慢
- ios - 我可以在真正的 iOS 设备上从 Windows 运行 React Native 应用程序吗?
- node.js - 如何使用 Node.js 在 Firebase 中使用电子邮件身份验证
- assembly - Tc2xx Tricore 的汇编语法
- python - django问题中的产品细节和图像
- python - 从服务运行脚本时不考虑 Ulimit 值
- amazon-web-services - AWS Route53 记录更新
- python - 组合数据集并对其进行格式化 Pandas Python