Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver findElement by xPath, no TIMEOUT if element is not found, screen just hang there.

I face some issue whereby the test just hang there (Browser open and not able to proceed next test) due to my test statement not able to find the element.

My TestStatemet like this:

driver.findElement(By.xpath("//input[@name='AID' and contains(@value,'sampleDataThatwillNotFound')]"));

The test hang only when find by XPATH, no issue when find by NAME/ID. I had set the timeout to 60 seconds, after the 60 seconds, it still hang.

Anyone out there facing this issue before? or Anyone got any idea how to resolve this issue?

like image 541
cL83 Avatar asked Dec 12 '25 14:12

cL83


2 Answers

Well, I had the same problem and found this in webdriver api doc: findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.

So I use something like

List<WebElement> found = driver.findElements(By.id("elementid"));
if (found.size() > 0) 
{
    // get the 1st element
} else {
    // time out
}

to resolve this issue. And implicit timeout works fine with findElements in my case.

like image 196
user2035144 Avatar answered Dec 14 '25 04:12

user2035144


I was having the same issue after upgrading Firefox (25 to 26) and Selenium (2.37.1 to 2.39.0 driver + server). No Exception thrown, hang forever, etc. It got "solved" by removing the implicitlyWait declaration. Not a real solution but good enough in my case.

like image 26
berbt Avatar answered Dec 14 '25 03:12

berbt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!