2

I have a testsuite, which uses WebDriver 3.5.2 ad ChromeDriver 2.31. I checked xpath for element in Google Chrome and I defined it in testcase. I set ChromeDriver in headless mode.

When I execute test I get follwoing error:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //*[@id="formA"]/p[1]/label (tried for 30 second(s) with 500 MILLISECONDS interval)

This xpath exists on webpage if I check it manually from browser. Webpage loads in a few seconds, so 30 should be enough.

plaidshirt
  • 5,189
  • 19
  • 91
  • 181
  • Can you share the URL of mentioned webpage? – Andersson Aug 30 '17 at 12:37
  • @Andersson : Sorry, it is only reachable from internal network. – plaidshirt Aug 30 '17 at 12:38
  • 1
    Check if there is any frame present – Shubham Jain Aug 30 '17 at 12:39
  • @ShubhamJain : I can't see any frames and it is working, when I execute it in a Portable Firefox and as I know xpath should be the same. – plaidshirt Aug 30 '17 at 12:42
  • cool .. so you problem is resolved? – Shubham Jain Aug 30 '17 at 12:43
  • Does the same code works with Firefox (geckodriver) or any other webdriver? – Andersson Aug 30 '17 at 12:43
  • Problem is not yet resolved, situation is the same. Yes, it is ok in Firefox. – plaidshirt Aug 30 '17 at 12:44
  • 2
    Try to [take screenshot of page](https://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver) or [get outerHTML](https://stackoverflow.com/questions/7263824/get-html-source-of-webelement-in-selenium-webdriver-using-python) of any `div` which is ancestor of target `label` to check whether it's actually present – Andersson Aug 30 '17 at 12:49
  • @Andersson : I get only a white png file as a screenshot. – plaidshirt Aug 30 '17 at 12:55
  • perhaps your website is not compatible with Chrome in headless mode? I have no idea how to test that. – Breaks Software Aug 30 '17 at 13:08
  • Maybe you problem is the same that this one: [link](https://stackoverflow.com/questions/31437515/selenium-stops-to-work-after-call-findelements). Also, check if the element exists inside the outerHTML as Andersson told you. – Striter Alfa Aug 31 '17 at 17:23
  • I checked it already, it is working perfectly, when I use browser in normal mode and not in headless. – plaidshirt Sep 01 '17 at 08:48
  • @plaidshirt try to get the outerHTML code debugging your test and try to find the desired node in a xml finding website, this way you can see if it was modifcated. If it is, just update your xpath or create a generic for both cases. If not... well, maybe try to change your xpath of a simplest and try again – Striter Alfa Sep 01 '17 at 13:40
  • @StriterAlfa : I don't know why should I check outerHTML. Test is working correctly in normal mode. So xpath should be ok. It is not working only in headless. Why should I change my code to make it working in headless browser too. – plaidshirt Sep 02 '17 at 20:34

1 Answers1

1

Try to use FluentWait. Pass the By test =By.xpath("//*[@id="formA"]/p[1]/label") to below function

WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}

Code for Explicit wait:

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("YOUR LOCTOR")));

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125