如何检查是否已经在Selenium中打开了一个特定端口的Chrome实例。

huangapple go评论79阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月7日 03:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定