简体   繁体   中英

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

I have the following code

        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();
        } 

I would like to check if there is already an instance of Chrome open with the port 9222 before executing. Is this possible? If so how would I go about doing this. Any help would be appreciated.

You can try the following:

Make an HTTP request to the url below

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

If selenium is not running on this port 9222 then by hitting above URL it will give you Unable to connect

If selenium server is already running on port 9222 then it will shut down the server and will give you OKOK .

You can then use these to handle execution as you desire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM