如何使用SeleniumWebDriver捕获网络流量并查找google analytics呼叫?

如何使用SeleniumWebDriver捕获网络流量并查找google analytics呼叫?,selenium,google-analytics,automation,automated-tests,Selenium,Google Analytics,Automation,Automated Tests,我需要使用自动化测试ga调用。 get(LogType.PERFORMANCE).getAll() 提供一组日志,但找不到呼叫请求url。 请帮助查找ga调用。要使用Python获取Selenium中的所有性能信息和网络调用,我使用execute\u script进行JS调用,该调用将返回性能日志: # Gets network information, including network calls def GetNetworkResources(driver): Resources

我需要使用自动化测试ga调用。

get(LogType.PERFORMANCE).getAll()

提供一组日志,但找不到呼叫请求url。

请帮助查找ga调用。

要使用Python获取Selenium中的所有性能信息和网络调用,我使用
execute\u script
进行JS调用,该调用将返回性能日志:

# Gets network information, including network calls
def GetNetworkResources(driver):
    Resources = driver.execute_script("return window.performance.getEntries();")
    for resource in Resources:
        print(resource['name'])
    return Resources

如果您需要Python以外的解决方案,我可以更新我的答案。

首先,用以下代码配置您的webdriver:

LoggingPreferences preferences = new LoggingPreferences();
preferences.enable(LogType.PERFORMANCE, Level.ALL);
ChromeOptions option = new ChromeOptions();
option.setCapability(CapabilityType.LOGGING_PREFS, preferences);
option.setCapability( "goog:loggingPrefs", preferences ); // for new chrome
WebDriver driver = new ChromeDriver(option);
关闭浏览器之前,请使用以下代码捕获文件中的日志:

OutputStream logfile = new FileOutputStream(new File("A text file path where you can save the logs"),true);
PrintStream printlog = new PrintStream(logfile);
            LogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
            for (LogEntry entry : logs) {
                 if(entry.toString().contains("\"type\":\"XHR\"") & entry.toString().contains("\"url\":\"https://example.com/")) {

                    printlog.append(new Date(entry.getTimestamp()) + " " + entry.toString() +" "+ System.getProperty("line.separator"));
                    printlog.append(System.getProperty("line.separator"));
                    printlog.append(System.getProperty("line.separator"));
                }
            }
            printlog.close();

此代码后,您可以关闭驱动程序。您将获得文件中的所有呼叫日志。

对于网络捕获,您可以使用BrowserMob代理或设置所需的功能来获取网络日志。您可以在这里找到详细信息:(sauce labs)这是否适用于Chrome以外的浏览器,或者这是ChromeDriver特有的?它应该适用于大多数浏览器,我相信
IE
Firefox
都具有
performance.getEntries()
功能,我们需要使用'goog:loggingpresfs'代替'CapabilityType.LOGGING\u PREFS'。关于更多信息,是的,感谢@BinnuJesudasanGudapati使用最新的Chrome,我们需要添加以下代码:option.setCapability(“goog:loggingpresfs”,首选项);编辑此代码。