簡體   English   中英

如何檢查 selenium 中是否已打開具有特定端口的 Chrome 實例

[英]How to check if there is a Chrome instance already open with a specific port in selenium

我有以下代碼

        try {
            System.setProperty("webdriver.chrome.driver", Sources.SOURCE_PATH + "chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
            driver = new ChromeDriver(options);
            ex = (JavascriptExecutor)driver;
        } catch (Exception e) {
            e.printStackTrace();
        } 

我想在執行之前檢查是否已經有一個使用端口 9222 打開的 Chrome 實例。 這可能嗎? 如果是這樣,我將如何 go 這樣做。 任何幫助,將不勝感激。

您可以嘗試以下方法:

向下面的 url 發出 HTTP 請求

import requests
res = requests.get('http://localhost:9222/selenium-server/driver/?cmd=shutDownSeleniumServer')

如果 selenium 未在此端口 9222 上運行,則通過點擊 URL 上方,它將給您Unable to connect

如果 selenium 服務器已經在端口 9222 上運行,那么它將關閉服務器並給你OKOK

然后,您可以根據需要使用這些來處理執行。

暫無
暫無

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

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