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(它是测试脚本的名称。)相应的错误如下:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)