簡體   English   中英

無法連接到 python 中的分離 selenium window

[英]Can`t attach to detached selenium window in python

無法在分離的 session 中向 selenium webdriver 發送命令,因為鏈接 http://localhost:port 死了。

但是如果我把斷點 1 鏈接保持活動狀態

import multiprocessing
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def create_driver_pool(q):
    options = Options()
    driver = webdriver.Chrome(options=options)
    pass #breakpoint 1
    return driver.command_executor._url

windows_pool = multiprocessing.Pool(processes=1)
result = windows_pool.map(create_driver_pool, [1])
print(result)
pass # breakpoint 2 for testing link

為什么會發生這種情況,我該怎么辦?

經過一番研究,我終於找到了這種行為的原因。 感謝https://bentyeh.github.io/blog/20190527_Python-multiprocessing.html和一些關於信號的谷歌搜索。 這根本不是信號。 我在 selenium.common.service 中找到了這段代碼

    def __del__(self):
        print("del detected")
        # `subprocess.Popen` doesn't send signal on `__del__`;
        # so we attempt to close the launched process when `__del__`
        # is triggered.
        try:
            self.stop()
        except Exception:
            pass

這是垃圾收集器 function 的處理程序,通過 SIGTERM 殺死子進程

                self.process.terminate()
                self.process.wait()
                self.process.kill()
                self.process = None

但是如果你在帶斷點的調試模式下,垃圾收集器不會收集這個 object, del不會啟動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM