2017-08-25 192 views
1

我用Selenium webdriver使用Java,我想知道是否有可能获得JSON体响应?我问,因为它有可能获得使用此代码JSON请求体:Java Selenium得到JSON响应体

driver.manage().logs().get(LogType.PERFORMANCE); 

,但我不能从响应得到的身体,但反应也存在于这个日志。有什么办法得到响应体?

+0

数据是JSON响应comimg ..你想达到 –

+0

请说明您的具体问题或添加额外的细节亮点是什么正是你所需要的。正如目前所写,很难确切地说出你在问什么。请参阅[问]页面以获得澄清此问题的帮助。 – JeffC

回答

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

因为y你想用Java来做到这一点。您需要使用BrowserMobProxy库为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 &middot; 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< 
+0

我不知道这种语言,我使用Java。在java的FirefoxProfile中我们没有函数set_proxy –

+0

@MateuszSobczak,请检查编辑 –

+0

这段代码不起作用。您可以检查https://stackoverflow.com/questions/45915525/browsermob-in-java-with-selenium-doesnt-work-different-errors-for-browsers –

1

我建议你看一下对于跟踪网络活动的专用工具,如Browsermob Proxy,这可能与硒轻松集成。内部硒日志不适合这种用途。

+0

感谢您的帮助,但是当我实现这个库到我的项目为Firefox浏览器发生错误org.openqa.selenium.SessionNotCreatedException:InvalidArgumentError:预期[对象未定义]未定义是一个整数 –

+0

@MateuszSobczak可能更好地检查BMP问题跟踪。可能是最新FF版本/驱动程序的一些问题。 –

+0

什么是BMP?我使用geckodriver 0.18.0和FIrefox ver 54.0.1 –