如何解决python多处理-共享相同的webdriver指针
我找不到正确的答案,所以我发布了这个问题。
理解问题的最快方法是目标:
有一个主进程和一个子进程(我要创建的一个)。主要过程通过webdriver检查多个网站,但有时它停留在硒水平较低的状态,并且不想更改官方代码。所以..我有时会手动检查监视器,以检查进程是否卡住,如果是,那么我会在浏览器中手动更改URL,它会再次正常运行。我不想成为人工检查人员。.所以我想使用一个子进程来自动化该任务,该子进程共享相同的webdriver,并通过webdriver.current_url
检查url并为我完成工作。
这是我尝试的最小代表示例形式,其中子进程仅检测到webdriver URL的更改
def test_sub(driver):
str_site0 = driver.current_url # get the site0 url
time.sleep(4) # give some time to the main-process to change to site1
str_site1 = driver.current_url # get the site1 url (changed by main-process)
if str_site0 == str_site1:
print('sub: no change detected')
else:
print('sub: change detected')
#endif
#enddef sub
def test_main():
""" main process changes from site0 (stackoverflow) to site1 (youtube)
sub process detects this change of url of the webdriver object (same pointer) by using
".current_url" method
"""
# init driver
pat_webdriver = r"E:\WPy64-3680\python-3.6.8.amd64\Lib\site-packages\selenium\v83_chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path= pat_webdriver)
time.sleep(2)
# open initial site
str_site0 = 'https://stackoverflow.com'
driver.get(str_site0)
time.sleep(2)
# init sub and try to pass the webdriver object
p = multiprocessing.Process(target=test_sub,args=(driver,)) # PROBLEM HERE! PYTHON UNCAPABLE
p.daemon = False
p.start()
# change site
time.sleep(0.5) # give some time sub query webdriver with site0
str_site1 = 'https://youtube.com' # site 1 (this needs to be detected by sub)
driver.get(str_site1)
# wait the sub to detect the change in url. and kill process (non-daemon insufficient don't know why..)
time.sleep(3)
p.terminate()
#enddef test_main
# init the program (main-process)
test_main()
通过执行$python test_multithread.py
(它是测试脚本的名称。)相应的错误如下:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。