Java – 如何使用Selenium獲取Network 網路請求

大家在用Java爬文章的時候是否會使用Selenium?

我在使用Selenium的時候碰到需要用到抓取Network的問題紀錄一下!

可以使用JavascriptExecutor來取得

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);

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *