Selenium中的pageLoadTimeout无效

我在Java中使用selenium使用下面的代码。 我正在添加4秒的pageLoadTimeout,但是,驱动程序继续等待,直到加载完整的页面。 有帮助吗?

System.setProperty("webdriver.gecko.driver", System.getProperty("user.home") + "\\Desktop\\geckodriver.exe"); FirefoxBinary b = new FirefoxBinary(new File(System.getProperty("user.home") + "\\desktop\\Mozilla Firefox\\firefox.exe")); FirefoxOptions options = new FirefoxOptions().setBinary(b); driver = new FirefoxDriver(options); //driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS); driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl"); List facilitySectionList = driver.findElements(By.className("facilitiesChecklistSection")); 

pageLoadTimeout问题的解决方案是将Selenium版本提升到v3.5.0 。 这是有效的代码块和结果org.openqa.selenium.TimeoutException: Timeout loading page after 2000ms在最小行中你自己的代码org.openqa.selenium.TimeoutException: Timeout loading page after 2000ms

代码块:

 public class Q45591282_pageloadtimeout { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver=new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS); driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl"); } } 

控制台输出:

 1502530864350 geckodriver INFO geckodriver 0.18.0 1502530864365 geckodriver INFO Listening on 127.0.0.1:29688 1502530865042 geckodriver::marionette INFO Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"] 1502530903170 Marionette INFO Listening on port 1900 Aug 12, 2017 3:11:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C Exception in thread "main" org.openqa.selenium.TimeoutException: Timeout loading page after 2000ms Build info: version: '3.5.0', revision: '8def36e068', time: '2017-08-10T23:00:22.093Z' System info: host: 'ATECHM-03', ip: '192.168.1.48', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.8.0_77' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{moz:profile=C:\Users\ATECHM~1\AppData\Local\Temp\rust_mozprofile.LSsvaNqlDbxE, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=6.2, moz:processID=3652.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}] Session ID: 8b841376-00fd-4359-8cae-a68912b23706 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641) at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:368) at demo.Q45591282_pageloadtimeout.main(Q45591282_pageloadtimeout.java:20) 

WebDriver.Timeouts – > pageLoadTimeout(long time,java.util.concurrent.TimeUnit unit)设置在抛出错误之前等待页面加载完成的时间。

pageLoadTimeout

 WebDriver.Timeouts pageLoadTimeout(long time,java.util.concurrent.TimeUnit unit) 

设置在抛出错误之前等待页面加载完成的时间。 如果超时为负,则页面加载可能是无限期的。 参数:time – 超时值。 unit – 时间单位。 返回:Timeouts接口。

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html

如果指定时间,并且selenium无法加载站点指定的时间,则会抛出TimedoutException 。 您应该在代码中处理它。

  driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS); long startTime = System.currentTimeMillis(); try{ driver.get("https://yahoo.com"); }catch (TimeoutException te){ long estimatedTime = System.currentTimeMillis() - startTime; System.out.println("it took "+estimatedTime+" Time"); } 

如果它没有抛出TimeoutException ,要么网站在指定时间内完成加载,要么Selenium API出错。