1

I have this code:

WebElement iframeElement = driver.findElement(By.xpath(xpIframe));  
driver.switchTo().frame(iframeElement);     

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpLucka)));
driver.findElement(xpLucka).click(); //this click fails
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpSvara)));  //TimeoutException
driver.findElement(xpSvar).click();
driver.findElement(xpSvara).click();

Frequently it fails on the line with the comment //TimeoutException. When I look at the state of the web page in the browser that is left open it is clear that the click on the line before it has failed. This is confusing. The element clearly is there, I find it without problems in the web inspector and the wait.until on the previous line obviously succeeds.

The next thing I want to make sure doesn't fail is the switchTo() statement. How can I verify a switchTo-call?

  • Note that is also succeeds frequently. I just ran this in a loop 9 times, it failed 5 times "but" succeeded 4 times.
  • Any other suggestions why this might happen are of course very welcome.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

To click() on an element within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for the desired elementToBeClickable.

  • You can use the following Locator Strategies:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("xpIframe")));
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("xpLucka"))).click();
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I faced some difficulties within my automation testing that WebDriverWait is not always working. As you may already know that Thread.sleep(3000); is not a recommended approach to use when it comes to automation testing, however sometimes you must use it. So for the testing purposes i would intentionally use:

Thread.sleep(3000); --> add some time, to make sure it switched to iframe
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));  
driver.switchTo().frame(iframeElement);     
Thread.sleep(3000);
driver.findElement(xpLucka).click(); --> see if it will click on element
Thread.sleep(3000);
driver.findElement(xpSvar).click();
Thread.sleep(3000);
driver.findElement(xpSvara).click(); --> same for all other elements
tifoso
  • 58
  • 6