我正在尝试阅读通过browsermob代理在硒重播期间遍历的页面源,但我总是得到一个空的har文件。
硒3.141browsermob核2.1.5 jar
以下是我的代码。
如果我设置
WebDriver驱动程序= new Chromedriver(options(;页面加载
WebDriver驱动程序=新Chromedriver(功能(;页面无法加载,返回页面未加载
代理按日志正确启动
/*********************************************************************************************************************************
[remotetestng]检测到的测试版6.14.3
slf4j:无法加载类" org.slf4j.impl.staticloggerbinder"。
slf4j:默认为no-Operation(nop(logger实现
slf4j:请参阅http://www.slf4j.org/codes.html#staticloggerbinder有关更多详细信息。
net.lightbody.bmp.browsermobproxyserver@613222f9dbrowsermobproxy59858is端口号
代理(手册,http = 10.88.16.64:59858,ssl = 10.88.16.64:59858(seleniumproxy
/******************************************/
任何建议都会有所帮助。
public void test() throws Exception
{
BrowserMobProxy proxy = getProxyServer(); //getting browsermob proxy
System.out.println(proxy+"BrowserMobProxy");
Proxy seleniumProxy = getSeleniumProxy(proxy);
System.out.println(seleniumProxy+"seleniumProxy");
DesiredCapabilities capabilities = new DesiredCapabilities().chrome();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", "C:\APMWS\testmvnproject\src\drivers\chromedriver76.exe");
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
proxy.newHar(); // creating new HAR
driver.get("https://google.com");
driver.navigate().to("https://pizzahut.com"); //example
List<HarEntry> entries = proxy.getHar().getLog().getEntries();
for (HarEntry entry : entries)
{
HarRequest request = entry.getRequest();
HarResponse response = entry.getResponse();
System.out.println(request.getUrl()+" : "+response.getStatus()+","+entry.getTime()+"ms");
}
proxy.stop();
driver.close();
}
public Proxy getSeleniumProxy(BrowserMobProxy proxyServer)
{
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
System.out.println(seleniumProxy.getHttpProxy());
try
{
String hostIp = Inet4Address.getLocalHost().getHostAddress();
System.out.println(hostIp);
seleniumProxy.setHttpProxy(hostIp + ":" + proxyServer.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + proxyServer.getPort());
System.out.println(proxyServer.getPort()+"is port number");
}
catch (UnknownHostException e)
{
e.printStackTrace();
System.out.println("invalid host");
}
return seleniumProxy;
}
public BrowserMobProxy getProxyServer()
{
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(0);
return proxy;
}
预期结果:写轨迹通过代理到har file
运行实际结果:代理设置,页面启动,但HAR文件为空。
您可以摆脱以下内容:SLF4J:无法加载类" org.slf4j.impl.staticloggerbinder"。
如果您正在运行Maven,则将其放入POM文件中。
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
这就是我在Chrome中启动HAR文件的方式。
// start the proxy
proxy = new BrowserMobProxyServer();
proxy.start(0);
// get the Selenium proxy object
org.openqa.selenium.Proxy seleniumProxy =
ClientUtil.createSeleniumProxy(proxy);
ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.PROXY, seleniumProxy);
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);