Selenium: Firefox with WebDriver: Page load issue
SeleniumServer version: 2.5.0, Firefox version: 4.0.1
I have a situation where a 'New Question' hyperlink is rendered through an Ajax call. Once the page load is complete, I need to click on this hyperlink to proceed. I'm using something like below to wait until the link is present and then click on it.
while (!(driver.findElement(By.xpath("//a[text()='New Question']")).isEnabled())) {
Thread.sleep(1000);
}
driver.findElement(By.xpath("//a[text()='New Question']")).click();
This works like a charm in IE. But in Firefox, the link is not clicked.
What this tells me is that Firefox is telling Selenium that the page is loaded when it is not loaded fully, where as IE is doing the right thing.
To ch开发者_高级运维eck whether the clicking is actually happening, I put in a javascript: alert("Hello From Chandra");
to the hyperlink's onclick. The pop-up showed up on IE, but not on Firefox.
Question: Am I doing something wrong/inadequate? Is there a workaround?
Thanks. PS: Please let me know if you need more info.
You don't need the while loop, you could use implicitlyWait when you initialize the driver. This will make the driver poll for the presence of an element for 90 seconds before throwing out element not found exception.
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[text()='New Question']")).click();
Thanks Nilesh and prestomanifesto, but I had tried 'WebDriverWait' and 'implicitlyWait' before putting in the while loop (which did not work as well) and it did not work. I did a bit more digging and 'seem' to have found a solution as follows. There is a 'search' text field that gets focus once the page has completed loading. I used the 'activeElement' to check if the focus is 'truly' set.
while (!driver.findElement(By.id("searchValue")).isEnabled()) {
WebElement currActiveElement = driver.switchTo().activeElement();
if (currActiveElement.getAttribute("id").equals("searchValue")) {
break;
}
Thread.sleep(1000);
}
You can still speedup your script execution by waiting for presence (not waiting for visibility, isEnabled) of expected element for say 5-8 sec and then sending window.stop() JS Script (to stop loading further elements ) without waiting for entire page to load or catching the timeout exception for page load after 5-8 seconds then calling window.stop()
Because if the page not adopted lazy loading technique (loading only visible element and loading rest of element only after scroll) it loads each element before returning window.ready state so it will be slower if any of the element takes longer time to render.
精彩评论