本文介紹了Selenium chromeDriver打開速度比直接在Chrome瀏覽器中打開網站慢得多的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在打開網站時遇到了一個關于Selenium Webdriver速度的令人厭惡的問題。
我正在測試的網站是內部網站,因此您無法訪問。為了詳細描述我的問題,我將該網站稱為ABC
。
當我在Chrome瀏覽器中鍵入ABC
的URL時,僅需1秒即可打開此網站。
在TestNG中,我的Selenium客戶端如下所示:
String ABC = "ABC'S URL";
String chromeDriverPath = "C:\selenium\chromedriver.exe" ;
System.out.println("start selenium");
File file = new File(chromeDriverPath);
System.setProperty("webdriver.chrome.driver",file.getAbsolutePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
webDriver driver = new ChromeDriver(options);
driver.get(ABC);
然后,Chrome將由自動化測試軟件控制。在示意圖上,會有一條注釋寫著waiting for staticxx.fackbook.com
或waiting for www.facebook.com
。
1分鐘后,ABC
網站已成功加載。我檢查了F12
工具,在控制臺中它顯示為staticxx.facebook.com/connect/xd_arbiter/r/0F7S7QWJ0Ac.js?version=42#channel=f38f3479a8af658&origin=http%
。
Failed to load resource: the server responded with a status of 503 (Service Unavailable)
有沒有什么Selenium API可以避免加載某些Web資源?
或者,我是否可以在瀏覽器上進行一些配置以停止加載某些Web資源?
提前感謝大家!
推薦答案
以下是您問題的答案:
為了避免加載某些網站,您可以利用Chrome瀏覽器的一個功能,調整pageLoadStrategy
到DesiredCapabilities
類,設置為none
,如下所示:
String ABC = "ABC'S URL";
String chromeDriverPath = "C:\selenium\chromedriver.exe" ;
System.out.println("start selenium");
File file = new File(chromeDriverPath);
System.setProperty("webdriver.chrome.driver",file.getAbsolutePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability("pageLoadStrategy", "none");
webDriver driver = new ChromeDriver(capabilities);
driver.get(ABC);
如果這回答了您的問題,請讓我知道。
這篇關于Selenium chromeDriver打開速度比直接在Chrome瀏覽器中打開網站慢得多的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,