Selenium chromeDriver 打开速度比直接在 chrome 浏览器中打开网站慢得多

Rog*_*Shu 6 java testng selenium page-load-time selenium-webdriver

我在打开网站时遇到了有关 Selenium Webdriver 速度的令人厌恶的问题。

我正在测试的网站是内部网站,因此您无法访问。为了详细描述我的问题,我将将该网站称为ABC

当我ABC在 Chrome 浏览器中输入 的 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);
Run Code Online (Sandbox Code Playgroud)

然后,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 资源?或者,我可以在浏览器上进行一些配置以停止加载某些网络资源吗?

谢谢大家!

Deb*_*anB 1

这是您问题的答案:

为了避免加载某些网站,您可以通过调整pageLoadStrategythrough DesiredCapabilitiesClass 来利用 Chrome 浏览器的一项功能,并将其设置为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);
Run Code Online (Sandbox Code Playgroud)

如果这能回答您的问题,请告诉我。