我使用Java和Selenium webdriver,我想知道是否可以获得JSON身体响应?我问,因为可以使用此代码获取JSON请求正文:
driver.manage().logs().get(LogType.PERFORMANCE);
但我无法从响应中获取正文,但此日志中也存在响应。有办法获得反应机构吗?
答案 0 :(得分:1)
我建议您查看用于跟踪网络活动的专用工具,例如Browsermob Proxy,它可以与Selenium轻松集成。内部硒原木不适用于此类目的。
答案 1 :(得分:1)
修改-2 强>
由于Geckodriver存在未解决的问题,https://github.com/mozilla/geckodriver/issues/764
因此,您需要使用解决方法并执行GeckoDriver在后台为您执行的操作
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.File;
import java.io.IOException;
/**
* Created by tarun.lalwani on 08/29/17.
*/
public class TestApp {
public static void main(String [] args) {
BrowserMobProxyServer proxyServer = new BrowserMobProxyServer();
proxyServer.start();
proxyServer.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
Proxy proxy = ClientUtil.createSeleniumProxy(proxyServer);
FirefoxProfile profile = new FirefoxProfile();
String host = proxy.getHttpProxy().split(":")[0];
int port = Integer.parseInt(proxy.getHttpProxy().split(":")[1]);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", host);
profile.setPreference("network.proxy.http_port", port);
profile.setPreference("network.proxy.ssl", host);
profile.setPreference("network.proxy.ssl_port", port);
FirefoxDriver driver = new FirefoxDriver(profile);
proxyServer.newHar("mysite");
driver.get("http://tarunlalwani.com");
Har har = proxyServer.getHar();
try {
har.writeTo(new File("har.json"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
修改-1 强>
因为你想在Java中这样做。你需要使用BrowserMobProxy libs for Java,下面的代码应该适合你
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;
.....
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy selProxy = ClientUtil.createSeleniumProxy(proxy);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, selProxy);
WebDriver driver = new FirefoxDriver(capabilities);
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.newHar("mysite");
driver.get("http://tarunlalwani.com");
Har har = proxy.getHar();
原始答案
您需要在Firefox上设置BrowserMobProxy。下面的代码适用于您
import time
from selenium import webdriver
from browsermobproxy import Server
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
server = Server("/path/to/bin/browsermob-proxy")
server.start()
# If sleep is not added sometime `create_proxy` throws an error
time.sleep(2)
proxy = server.create_proxy()
sel_proxy = proxy.selenium_proxy()
profile = FirefoxProfile()
profile.set_proxy(sel_proxy)
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("mysite", options={'captureHeaders': True, 'captureContent': True} )
driver.get("http://tarunlalwani.com")
print(proxy.har)
这将提供如下输出
'version': '1.2',
'creator': {
'name': 'BrowserMob Proxy',
'version': '2.1.4',
'comment': ''
},
'pages': [
{
'id': 'mysite',
'startedDateTime': '2017-08-25T21:38:08.934+05:30',
'title': 'mysite',
'pageTimings': {
'comment': ''
},
'comment': ''
}
],
'entries': [
{
'pageref': 'mysite',
'startedDateTime': '2017-08-25T21:38:09.367+05:30',
'request': {
'method': 'GET',
'url': 'http://tarunlalwani.com/',
'httpVersion': 'HTTP/1.1',
....
'response': {
'status': 200,
'statusText': 'OK',
'httpVersion': 'HTTP/1.1',
'cookies': [
],
'content': {
'size': 21336,
'mimeType': 'text/html; charset=utf-8',
'text': '<!DOCTYPE html>\n<html lang="en">\n<head prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article# website: http://ogp.me/ns/website#">\n <meta http-equiv="content-type" content="text/html; charset=utf-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">\n \n \n <meta name="description" content="TARUN LALWANI">\n <meta name="twitter:description" content="TARUN LALWANI">\n \n\n <meta property="og:title" content="TARUN LALWANI">\n <meta property="twitter:title" content="TARUN LALWANI">\n \n <meta property="og:type" content="website">\n \n <meta property="og:description" content="">\n <meta property="og:url" content="http://tarunlalwani.com/">\n <meta property="og:site_name" content="TARUN LALWANI">\n \n\n \n\n \n\n <meta name="generator" content="Hugo 0.25.1" />\n <title>TARUN LALWANI · TARUN LALWANI</title>\n \n <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css">\n <link rel="stylesheet" href="http://tarunlalwani.com/css/style.css">\n\n \n\n \n <link href="http://tarunlalwani.com/index.xml" rel="alternate" type="application/rss+xml" title="TARUN LALWANI" />\n \n \n\n \n \n</head>\n<body>\n\n<nav class="navbar navbar-default navbar-fixed-top visible-xs">\n\t<div class="container-fluid">\n\t\t<div class="navbar-header">\n\t\t\t<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">\n\t\t\t\t<span class="sr-only">Toggle navigation</span>\n\t\t\t\t<span class="icon-bar"></span>\n\t\t\t\t<span class="icon-bar"></span>\n\t\t\t\t<span class="icon-bar"></span>\n\t\t\t</button>\n\t\t\t\n\t\t\t\t<a class="navbar-brand" href="http://tarunlalwani.com/">TARUN LALWANI</a>\n\t\t\t\n\t\t</div>\n\t\t<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">\n\t\t\t<ul class="nav navbar-nav">\n\t\t\t\t\n\t\t\t\t\n\t\t\t</ul>\n\t\t</div>\n\t</div>\n</nav>\n<div class="container-fluid">\n\t<div class="row">\n\t\t<div id="menu" class="hidden-xs col-sm-4 col-md-3">\n\t<div id="menu-content" class="vertical-align">\n\t\t\n\t\t\t<