英文:
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 实例在运行。这可行吗?如果可以,我应该如何操作?任何帮助将不胜感激。
英文:
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.
答案1
得分: 1
以下是翻译好的内容:
您可以尝试以下操作:
向下面的 URL 发送 HTTP 请求
import requests
res = requests.get('http://localhost:9222/selenium-server/driver/?cmd=shutDownSeleniumServer')
如果 Selenium 没有在此端口 9222 上运行,则通过访问上述 URL 会返回
无法连接
如果 Selenium 服务器已在端口 9222 上运行,则它将关闭服务器并返回
OKOK
。
然后,您可以根据需要使用这些内容来处理执行。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论