如何使用selenium访问Google Chrome开发者工具上的网络面板?

Tes*_*ter 23 networking selenium google-chrome-devtools selenium-webdriver

我想在开发人员工具上获得网络面板的输出.[网络面板 - >名称,方法,状态,类型,Intiator,大小,时间,时间线]我需要这些信息.

ser*_*yan 24

这可以通过Selenium WebDriver实现.为此,您应该执行以下操作:

  1. http://docs.seleniumhq.org/download/下载特定于selenium语言的客户端驱动程序,并将适当的jar文件添加到项目构建路径中.

  2. 要使用Chrome/Chromium运行测试,您还需要可以从http://chromedriver.storage.googleapis.com/index.html下载的chromdriver binary.

  3. 创建一个这样的测试用例:

    // specify the path of the chromdriver binary that you have downloaded (see point 2)
    System.setProperty("webdriver.chrome.driver", "/root/Downloads/chromedriver");
    ChromeOptions options = new ChromeOptions();
    // if you like to specify another profile
    options.addArguments("user-data-dir=/root/Downloads/aaa"); 
    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();
Run Code Online (Sandbox Code Playgroud)

在Chrome/Chromium上执行javascript将帮助您获取网络(不仅仅是)信息.结果字符串'netData'将包含JSONArray格式的所需数据.

希望这会有所帮助.

  • 没有网络。有表现。关于被阻止资源的信息呢? (2认同)
  • 当我尝试这个时,我可以获得前 150 个网络响应,我如何获得完整的响应详细信息。 (2认同)

Mad*_*nha 9

这个答案.

您可以使用LoggingPreferences获取性能日志.它以json格式返回数据.这是一个示例Java代码.使用硒2.53,chromedriver 2.20,Ubuntu 14.04上的Chrome 50进行测试.这也适用于Windows.

    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("http://www.google.com");
    LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
    for (LogEntry le : les) {
        System.out.println(le.getMessage());
    }
Run Code Online (Sandbox Code Playgroud)

这是一个示例输出.它是手动格式化的.实际的输出是一行的.

{
    "message": {
        "method": "Network.requestWillBeSent",
        "params": {
            "documentURL": "https://www.google.co.in/?gfe_rd=cr&ei=gpwxV4OSKMmR2ASEg6-YCg&gws_rd=ssl",
            "frameId": "31172.2",
            "initiator": {
                "stack": {
                    "callFrames": [
                        {
                            "columnNumber": 11511,
                            "functionName": "",
                            "lineNumber": 55,
                            "scriptId": "50",
                            "url": "https://www.google.co.in/?gfe_rd=cr&ei=gpwxV4OSKMmR2ASEg6-YCg&gws_rd=ssl"
                        }
                    ]
                },
                "type": "script"
            },
            "loaderId": "31172.3",
            "request": {
                "headers": {
                    "Accept": "*/*",
                    "Referer": "https://www.google.co.in/",
                    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"
                },
                "initialPriority": "Low",
                "method": "GET",
                "mixedContentType": "none",
                "url": "https://www.google.co.in/xjs/_/js/k=xjs.s.en.VTDhrkH4c9U.O/m=sx,c,sb,cdos,cr,elog,jsa,r,hsm,qsm,j,p,d,csi/am=AJQ0CwoS8fchIGwhrCA1YGBR/rt=j/d=1/t=zcms/rs=ACT90oGi2YIjVL5cBzOc1-MD37a1NqZ1jA"
            },
            "requestId": "31172.3",
            "timestamp": 251208.074288,
            "type": "Other",
            "wallTime": 1462869123.92204
        }
    },
    "webview": "8AF4A466-8027-4340-B9E9-CFEBDA769C50"
}
Run Code Online (Sandbox Code Playgroud)

  • 有办法获得响应机构吗? (5认同)
  • 我也在寻找一种方法来获得响应体。似乎不可能。 (3认同)

kak*_*ion 8

假设您要加载页面(例如google.com)并提取资源计时对象数组(即window.performance.getEntries()):

import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver)
driver.get('https://www.google.com');
time.sleep(5)
timings = driver.execute_script("return window.performance.getEntries();")
print timings
Run Code Online (Sandbox Code Playgroud)


use*_*363 6

对于 Python,一种方法是:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import chromedriver_binary # If you're using conda like me.

yoururl = "www.yoururl.com"

caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)

driver.get(yoururl)
time.sleep(10) # wait for all the data to arrive. 
perf = driver.get_log('performance')

Run Code Online (Sandbox Code Playgroud)

perf是一个字典列表,您将能够在此列表中找到您要查找的项目。即,字典是您在 Chrome 开发工具网络选项卡中看到的内容。


Jas*_*don 5

如其他答案所述,您需要利用window.performance方法。

getEntries()
getEntriesByType()
getEntriesByName()
» 条目类型

例如,我在nodejs Selenium-WebDriverChromedriver测试中使用了以下代码段来收集google analytics网络调用:

driver
  .executeScript( "return window.performance.getEntriesByType('resource');" )
  .then( (perfEntries)=> {
    let gaCalls = perfEntries.filter(function(entry){
      return /collect\?/i.test(entry.name);
    });
    console.log(gaCalls);
});
Run Code Online (Sandbox Code Playgroud)

如果使用getEntries()代替getEntriesByType('resource'),它将返回...所有条目。