Selenium chrome 驱动程序打开比直接在 chrome 浏览器中打开网站慢得多

Selenium chromeDriver open extremely slower than open a website in chrome browser directly

我在打开网站时遇到了一个关于 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.comwaiting 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 资源? 或者,我可以在浏览器上进行一些配置以停止加载某些网络资源吗?

提前谢谢大家!

这是您问题的答案:

为了避免加载某些网站,您可以利用 Chrome 浏览器的一项功能,方法是调整 pageLoadStrategyDesiredCapabilities Class 并将其设置为 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);

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