Java 使用 Selenium 如何获取网络请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45847035/
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
Using Selenium how to get network request
提问by zsbappa
采纳答案by Shubham Jain
Not exactly open by dev tools but found some network, performance and other results.
不完全由开发工具打开,但发现了一些网络、性能和其他结果。
Yes you can do that using JavascriptExecutor
是的,您可以使用 JavascriptExecutor 做到这一点
Code is as below :-
代码如下:-
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");
String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);
OR
或者
DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
System.out.println(le.getMessage());
}
The first code retrun network return network;"
because of this JS tag. You can remove JS code of entity which you don't require
第一段代码return network;"
因为这个JS标签重跑网络。您可以删除不需要的实体的 JS 代码
The second code return perfromance
第二个代码返回性能
Hope it will help you :)
希望它会帮助你:)
回答by naveen kumar
- You can use "browsermob-proxy", "LoggingPreferences", "CloseableHttpClient", "HttpURLConnection" for getting the logs
- If you wish not to use browser and want to get the response, then I would suggest to go for "CloseableHttpClient".
- Copy the URI ("www.somewebsite.com/v1/api/sign-in?"). Get the request payload(which will be available in that particular API URI). Pass all the parameters with "&" like this "www.somewebsite.com/v1/api/sign-in?&username=xyz&password=1234566&app_id=12123214324234134&app_secret=213242345345345" (Remember app id and app secret is very unique and do not expose it anywhere)
- Once you get the URI, this code will give you JSON format response
- 您可以使用“browsermob-proxy”、“LoggingPreferences”、“CloseableHttpClient”、“HttpURLConnection”来获取日志
- 如果您不想使用浏览器并希望获得响应,那么我建议您选择“CloseableHttpClient”。
- 复制 URI(“www.somewebsite.com/v1/api/sign-in?”)。获取请求有效负载(它将在该特定 API URI 中可用)。使用“&”传递所有参数,如“www.somewebsite.com/v1/api/sign-in?&username=xyz&password=1234566&app_id=12123214324234134&app_secret=213242345345345”(记住应用程序是唯一的,不要暴露应用程序的秘密任何地方)
- 获得 URI 后,此代码将为您提供 JSON 格式的响应
HttpPost request = new HttpPost(str);
request.setHeader("content-type", "application/json");
HttpResponse response = client.execute(request);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
while ((line = bufReader.readLine()) != null) {
builder=String.valueOf(line);
}
System.out.println(builder);
}
回答by Norayr Sargsyan
It's working for me
它对我有用
ChromeOptions options = new ChromeOptions();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable( LogType.PERFORMANCE, Level.ALL );
options.setCapability( "goog:loggingPrefs", logPrefs );
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
List<LogEntry> entries = driver.manage().logs().get(LogType.PERFORMANCE).getAll();
System.out.println(entries.size() + " " + LogType.PERFORMANCE + " log entries found");
for (LogEntry entry : entries) {
System.out.println(entry.getMessage());
}