问题
I am testing a website in linux host.The page i am accessing loads infinitely so I am trying to set pageLoadTimeout for selenium. Firefox is triggered correctly but URL is not loading/navigating/added in url bar.just blank firefox window.I am not seeing any errors also.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
driver.get("http://www.example.com");
However if I remove driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
code is working fine
Selenium version : 3.14.0;
gecko driver : 18 - linux (tested with gecko 16,17 also same issue)
browser : firefox-52
os/platform : linux
If this kind of some issue how do I make sure my driver quit itself after 5 minute.Host will support only firefox 52.
I checked this link but doesnt fix my problem.
Thanks Jk
回答1:
You can set the pageload strategy for browser which will then make the page not wait for the full page load for your other Selenium commands to be executed. Below is the sample code snippet in Java. There are three supported values:
normal
This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).
eager
This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).
none
This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).
By default, when Selenium loads a page, it follows the normal pageLoadStrategy.
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("pageLoadStrategy", "eager");
FirefoxOptions opt = new FirefoxOptions();
opt.merge(caps);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com/");
If you are interested only in the HTML of the page, better use the "eager" strategy.
回答2:
You haven't mentioned the url you are trying to access but pageLoadTimeout for Selenium works as expected with With Selenium v3.14.0, GeckoDriver v0.23.0 and Firefox Quantum v62.0.3 combination. I am able to see the expected output on the console with the following example which prints TimeoutException occurred. Quiting the program whenever the pageLoadTimeout is triggered:
Code Block:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.TimeoutException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class A_Firefox_Test { public static void main(String[] args) { System.setProperty("god.bless.us", "C:/Utility/BrowserDrivers/geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS); try { 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"); } catch (TimeoutException e) { System.out.println("TimeoutException occurred. Quiting the program."); } driver.quit(); } }
Console Output:
1539157195615 Marionette INFO Listening on port 1920 Oct 10, 2018 1:09:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C Oct 10, 2018 1:10:00 PM org.openqa.selenium.remote.ErrorCodes toStatus INFO: HTTP Status: '500' -> incorrect JSON status mapping for 'timeout' (408 expected) TimeoutException occurred. Quiting the program.
You can find the detailed stack trace in pageLoadTimeout in Selenium not working
- You can find the Pythonic approach to
pageLoadTimeout
in How to set the timeout of 'driver.get' for python selenium 3.8.0?
来源:https://stackoverflow.com/questions/52726804/pageloadtimeout-is-not-working-in-selenium-java