Selenium (Chrome) 和 BrowserMob 不适用于 https
Selenium (Chrome) and BrowserMob doesn't work for https
我一直在尝试将 BrowserMob 集成到我的 selenium 测试中。它适用于在 http 上运行的网站,但对于 https 网站,浏览器停止工作并且 HAR 文件不包含任何请求。
导航到 https 站点时,我在浏览器上收到此错误。
"There is something wrong with the proxy server or the address is incorrect."
这是我的代码。
public class Browsermob {
BrowserMobProxy proxy = new BrowserMobProxyServer();
@Test
public void browsermobtest() {
proxy.start(9091);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
// create a new HAR with the label "google.com"
proxy.newHar("http://www.google.com/");
// open google.com
driver.get("https://www.google.ee/#gfe_rd=cr");
driver.findElement(By.cssSelector("#gb_70")).click();
}
@AfterMethod
public void Afterthetest() {
// get the HAR data
Har har = proxy.getHar();
File harFile = new File("C:/Users/Madis/Documents/har.har");
try {
har.writeTo(harFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
您混淆了浏览器 mob 代理对象和 selenium 代理对象。
您的代理变量 proxy
是您的浏览器将连接到的实际代理。
您的 seleniumProxy
变量是一个代表浏览器代理设置的对象。
您告诉您的浏览器使用 "trustAllSSLCertificates" 作为代理服务器的地址,这就是您收到错误的原因。相反,您应该告诉 browsermob (proxy
) 到 trustAllSSLCertificates
,并且您的 sslProxy 需要引用您的 browsermob 代理。
像这样启动代理:
public void startProxy() {
proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(9091);
}
像这样启动驱动程序:
public void startBrowserWithProxy() {
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
seleniumProxy.setSslProxy("localhost:" + proxy.getPort());
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
}
您不需要在 Selenium 代理对象上指定 sslProxy。 ClientUtil.createSeleniumProxy
为你做这个,在 大多数 简单的情况下,它选择一个合适的默认值(使用 InetAddress.getLocalHost() ;如果这对 HTTP 有效,它会工作也适用于 HTTPS)。
需要记住的几点:
- 您将在浏览器中收到 SSL 警告,除非您告诉浏览器忽略证书错误(在 Chrome 上,使用 --ignore-certificate-errors 命令行标志),或安装BMP CA 在浏览器的信任库中(对于 Windows 上的 Chrome,您必须将其安装在 Windows 信任库中。
- 根据您的 Chrome 和 OS 版本,您可能需要使用命令行选项指定备用用户数据目录。例如,
--user-data-dir=/tmp/insecurechrome
.
- BMP 有自己的可信证书来源(Java 信任库 + 来自 Mozilla 的最新列表),因此如果您尝试使用私人 CA 颁发的证书连接到内部网站,您需要告诉 BMP 要么信任私有 CA,要么使用 .setTrustAllServers(true) 跳过证书验证。
- 必须在调用 createSeleniumProxy() 之前使用 .start(...) 启动代理。
将所有这些结合起来,您的代码将如下所示:
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(9091);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// NOTE: there is no call to .setSslProxy() here
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArgument("--ignore-certificate-errors");
// replace 'somedirectory' with a suitable temp dir on your filesystem
options.addArgument("--user-data-dir=somedirectory");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
// [...]
我设法让它工作了。添加 log4j 并调试 browsermob 日志后,问题是由
引起的
Caught an exception on ClientToProxyConnection
java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.fromHost(Ljava/lang/String;)Lcom/google/common/net/HostAndPort;
为了让它工作,我必须向我的 maven 项目添加一个依赖项。这解决了这个问题,我能够看到捕获 https 站点和 http 站点上的流量。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
我遇到了这个问题。经过多次试验,我知道如果您连接到公司代理,我们必须添加 setmitmManager 和上游代理。它对我有用。
这是示例代码。
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
//Add below line if you are under corporate proxy.
proxy.setChainedProxy(new InetSocketAddress("XXX.XXX.com", 8080));
proxy.setMitmManager(ImpersonatingMitmManager.builder().trustAllServers(true).build());
proxy.start(9091);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver","C:/Users/Madis/Documents/chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
// your code to start, get har
我有很多功能、选项等,但没有用
就我而言,我更改了 pom
中的存在依赖项
<artifactId>browsermob-core-littleproxy</artifactId>
到
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
</dependency>
及 "guava" 版本。
之后一切都变好了
我一直在尝试将 BrowserMob 集成到我的 selenium 测试中。它适用于在 http 上运行的网站,但对于 https 网站,浏览器停止工作并且 HAR 文件不包含任何请求。
导航到 https 站点时,我在浏览器上收到此错误。
"There is something wrong with the proxy server or the address is incorrect."
这是我的代码。
public class Browsermob {
BrowserMobProxy proxy = new BrowserMobProxyServer();
@Test
public void browsermobtest() {
proxy.start(9091);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
// create a new HAR with the label "google.com"
proxy.newHar("http://www.google.com/");
// open google.com
driver.get("https://www.google.ee/#gfe_rd=cr");
driver.findElement(By.cssSelector("#gb_70")).click();
}
@AfterMethod
public void Afterthetest() {
// get the HAR data
Har har = proxy.getHar();
File harFile = new File("C:/Users/Madis/Documents/har.har");
try {
har.writeTo(harFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
您混淆了浏览器 mob 代理对象和 selenium 代理对象。
您的代理变量 proxy
是您的浏览器将连接到的实际代理。
您的 seleniumProxy
变量是一个代表浏览器代理设置的对象。
您告诉您的浏览器使用 "trustAllSSLCertificates" 作为代理服务器的地址,这就是您收到错误的原因。相反,您应该告诉 browsermob (proxy
) 到 trustAllSSLCertificates
,并且您的 sslProxy 需要引用您的 browsermob 代理。
像这样启动代理:
public void startProxy() {
proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(9091);
}
像这样启动驱动程序:
public void startBrowserWithProxy() {
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
seleniumProxy.setSslProxy("localhost:" + proxy.getPort());
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
}
您不需要在 Selenium 代理对象上指定 sslProxy。 ClientUtil.createSeleniumProxy
为你做这个,在 大多数 简单的情况下,它选择一个合适的默认值(使用 InetAddress.getLocalHost() ;如果这对 HTTP 有效,它会工作也适用于 HTTPS)。
需要记住的几点:
- 您将在浏览器中收到 SSL 警告,除非您告诉浏览器忽略证书错误(在 Chrome 上,使用 --ignore-certificate-errors 命令行标志),或安装BMP CA 在浏览器的信任库中(对于 Windows 上的 Chrome,您必须将其安装在 Windows 信任库中。
- 根据您的 Chrome 和 OS 版本,您可能需要使用命令行选项指定备用用户数据目录。例如,
--user-data-dir=/tmp/insecurechrome
. - BMP 有自己的可信证书来源(Java 信任库 + 来自 Mozilla 的最新列表),因此如果您尝试使用私人 CA 颁发的证书连接到内部网站,您需要告诉 BMP 要么信任私有 CA,要么使用 .setTrustAllServers(true) 跳过证书验证。
- 必须在调用 createSeleniumProxy() 之前使用 .start(...) 启动代理。
将所有这些结合起来,您的代码将如下所示:
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(9091);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// NOTE: there is no call to .setSslProxy() here
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArgument("--ignore-certificate-errors");
// replace 'somedirectory' with a suitable temp dir on your filesystem
options.addArgument("--user-data-dir=somedirectory");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
// [...]
我设法让它工作了。添加 log4j 并调试 browsermob 日志后,问题是由
引起的Caught an exception on ClientToProxyConnection
java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.fromHost(Ljava/lang/String;)Lcom/google/common/net/HostAndPort;
为了让它工作,我必须向我的 maven 项目添加一个依赖项。这解决了这个问题,我能够看到捕获 https 站点和 http 站点上的流量。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
我遇到了这个问题。经过多次试验,我知道如果您连接到公司代理,我们必须添加 setmitmManager 和上游代理。它对我有用。
这是示例代码。
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
//Add below line if you are under corporate proxy.
proxy.setChainedProxy(new InetSocketAddress("XXX.XXX.com", 8080));
proxy.setMitmManager(ImpersonatingMitmManager.builder().trustAllServers(true).build());
proxy.start(9091);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver","C:/Users/Madis/Documents/chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
// your code to start, get har
我有很多功能、选项等,但没有用 就我而言,我更改了 pom
中的存在依赖项<artifactId>browsermob-core-littleproxy</artifactId>
到
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
</dependency>
及 "guava" 版本。 之后一切都变好了