Java 需要在 selenium webdriver 代码中使用代理捕获网络流量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20899331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to capture network traffic using proxy in selenium webdriver code
提问by Lingaraj R M
Need capture network traffic using proxy in selenium webdriver code.. I've tried with below code but after opening browser google.com is not loading getting the error "proxy server that is refusing connections"
需要使用 selenium webdriver 代码中的代理捕获网络流量。
public class Test_One {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ProxyServer server = new ProxyServer(8090);
server.start();
server.setCaptureHeaders(true);
server.setCaptureContent(true);
server.newHar("test");
DesiredCapabilities capabilities = new DesiredCapabilities();
Proxy proxy = server.seleniumProxy();
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 8090);
profile.setPreference("network.proxy.ssl", "localhost");
profile.setPreference("network.proxy.ssl_port", 8090);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.no_proxies_on", "");
profile.setProxyPreferences(proxy);
capabilities.setCapability(FirefoxDriver.PROFILE,profile);
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
Har har1 = server.getHar();
server.stop();
driver.quit();
}
}
回答by ankushagarwal
The latest version of Selenium Webdriver does not really support traffic capture. You can however, use BrowserMob proxy to capture traffic. https://github.com/lightbody/browsermob-proxy. The README has examples on how to do that with Selenium.
最新版本的 Selenium Webdriver 并不真正支持流量捕获。但是,您可以使用 BrowserMob 代理来捕获流量。https://github.com/lightbody/browsermob-proxy。README 中有关于如何使用 Selenium 做到这一点的示例。
回答by Lukasz A
Similar topic: https://stackoverflow.com/a/55202231/2917470
类似话题:https: //stackoverflow.com/a/55202231/2917470
You can try moxproxy to capture or modify http traffic - githubrepo
可以尝试使用 moxproxy 来捕获或修改 http 流量 - githubrepo
Examples here
这里的例子