python - 多线程硒python
问题描述
我正在尝试在 selenium 中应用线程以同时运行 3 个浏览器并完成这项工作,但它丢失了线程会话,所以你可以说我没有弄清楚如何做到这一点代码
这是 init_sele_chrom.py 文件
from selenium.webdriver.chrome.options import Options
from webdriver_manager.firefox import GeckoDriverManager
from seleniumwire import webdriver
class init_sele_chrom():
def __init__(self):
self.driver = self.init_driver()
def renw_check(self):
self.driver.close()
self.driver = self.init_driver()
def init_driver(self,hidden_val=False):
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
options.add_argument("--log-level=0")
options.add_argument("--log-level=1")
options.add_argument("--log-level=2")
options.add_argument("--log-level=3")
options.add_argument("--incognito")
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
if hidden_val:
# print('from hidden browser')
options.headless = True
else:
options.headless = False
options.add_argument("--start-maximized")
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.set_window_size(600,950)
return driver
这是初始化文件 init_prog.py
class init_prog():
def __init__(self):
self.driver = init_sele_chrom()
self.data_serch =data_serch(driver)
这是数据搜索文件 data_serch.py
class data_serch():
def __init__(driver):
self.driver = driver
def data_serch_func(self,data):
# here is the code for the search to go to a web site and meke the event and do the job and pass the data
pass
这是线程文件 main_thread_file.py
from datetime import datetime
import colorama
from colorama import init, Fore, Back, Style
from Socket_Singleton import Socket_Singleton
import concurrent.futures
from init_prog import init_prog
colorama.init(autoreset=True)
class main_control():
def __init__(self):
self.operation()
def test(self, index,data):
now = datetime.now()
# get the current time with format
current_time = now.strftime("%H:%M:%S")
# print the current time
print("Current Time =", current_time)
if index == 0 or index == 1:
self.count_loop = 0
else:
self.count_loop += 1
if self.count_loop == 0:
self.init_pro = init_prog()
self.init_pro.data_serch_func(data)
def operation(self):
self.count_loop = 0
data_list = ['','','','','','','','']
with concurrent.futures.ThreadPoolExecutor(2) as executor:
res = [executor.submit(self.test,index,data)
for index,data in enumerate(data_list)]
concurrent.futures.wait(res)
# print finish from the operation
print(Fore.GREEN+'Finish...' + Style.RESET_ALL)
main_control()
解决方案
推荐阅读
- javascript - jsconfig.json 允许在 React 中进行绝对导入,但会破坏 Styleguidist 导入
- python - python中的文件搜索
- flutter - 如何解决未处理的异常:FormatException:在 Flutter 中下载文件时找不到中央目录记录的结尾?
- arrays - 无法解码来自 AWS 的网络响应
- html - Aria 标签和 Alt 属性
- python - 绘制由缺失值组成的数据集的平均线性回归
- apache-kafka - 如何使用具有特定分区器的 Apache Flink 将数据作为键/值发送到 Kafka
- python - 根据分隔符吐出一列
- reactjs - npm 运行版本 2.15.12 而不是 windows 中的最新版本 6
- java - How to map a collection Map
> type using Hibernate