Issue clicking on an iframe element using WebDriver

Hi folks,

I am having an issue clicking on an iframe element using WebDriver and was hoping someone might be able to help me.

I’ve tried a number of findElement methods but am not having any success.

When I use the ‘Chrome Dev’ tool I am seeing that the item is found (see attached screen-shot).

Error message from Katalon after test case execution:

Test Cases/Webdriver to clicks on page iframe element FAILED because (of) (Stack trace: org.openqa.selenium.NoSuchElementException:

no such element: Unable to locate element: {“method”:“xpath”,“selector”:"//a[contains(@href,’&cmp=money-box&lnk=money-box-nse&nsrv1=ushome’)]"}

Configuration:

OS: Windows 7

Katalon Studio: Version: 5.8.0: Build: 1

Chrome: Version 70.0.3538.67 (Official Build) (64-bit)

import org.openqa.selenium.By as Byimport org.openqa.selenium.WebDriver as WebDriverimport com.kms.katalon.core.webui.driver.DriverFactory as DriverFactoryimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI//Used for clicking NSE iframe link on http://www.rediff.comWebUI.openBrowser('http://www.rediff.com/')WebUI.delay(2)WebDriver driver = DriverFactory.getWebDriver()WebUI.delay(2)//--This worksdriver.switchTo().frame(driver.findElement(By.cssSelector("iframe[name='moneyiframe']"))); WebUI.delay(3)//-- This is not working but Chrome Dev tool finds itdriver.findElement(By.xpath("//a[contains(@href,'&cmp=money-box&lnk=money-box-nse&nsrv1=ushome')]")).click(); driver.switchTo().defaultContent()

IframeNotWorking.png

IframeNotWorking2.png

Your page has a lot of iframe elements. It would take time to load them complete. I guess WebUI.delay(3) before driver.findElement(By.xpath("//a…")) is not long enough. How about trying WebUI.delay(10)?

I would suggest using WebDriver’s Fluent Wait than fixed length of seconds by WebUI.delay(X). See

  •   https://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java
    

I would rather use WebUI.verifyElementPresent(TestObject to, int timeout) which perfoms wait with timeout

Thanks for your fast feed-back.

Just for testing purposes i set WebUI.delay(60), same issue “Unable to locate element.”
So not sure it’s a waiting issue?

Cheers,
Dave

I tried on my side to run the test just as you pasted above.
It ran successful without org.openqa.selenium.NoSuchElementException.

Configuration:

OS: Windows 7

Katalon Studio: Version: 5.8.0: Build: 1

Firefox 62.0.3

Thank you for trying my test.

It’s strange that it’s not working for me. I will give it a try again. I will change some of my delays to see if it makes a difference.

Cheers,
Dave

The following post would help you.

Hi kazurayam,

I ran the same test in Firefox like you did and it worked for me too.
The issue seems to be that ‘driver.findElement(By.xpath("")).click()’ is not working for Chrome in the latest version of Katalon. Not sure if there is a work-around for this or not? I will post the issue in the defects section so that the Katlon folks can look into it. Thanks for your help.

Paste the following code into a new test case and execute:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObjectimport org.openqa.selenium.By as Byimport org.openqa.selenium.WebDriver as WebDriverimport com.kms.katalon.core.webui.driver.DriverFactory as DriverFactoryimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI//Used for clicking NSE iframe linkWebUI.openBrowser('http://www.rediff.com/')WebDriver driver = DriverFactory.getWebDriver()driver.switchTo().frame(driver.findElement(By.cssSelector('iframe[name=\'moneyiframe\']')))WebUI.delay(10) //This delay type used so others can paste my code to reproduce my error//This WORKS in Firefox Quantum Version 62.0.3 (64-bit)//This DOES NOT work in Chrome Version 70.0.3538.67 (64-bit)driver.findElement(By.xpath('//a[contains(@href,\'&cmp=money\')]')).click()driver.switchTo().defaultContent()WebUI.closeBrowser()

After updating to the newest ChromeDriver.exe iframe object searching works again in Katalon.
Cheers,
Dave

Dave Evers said:

After updating to the newest ChromeDriver.exe iframe object searching works again in Katalon.
Cheers,
Dave

Thanks for this Dave and kazurayam!

I can confirm that the latest (currently v2.43) ChromeDriver.exe is able to find elements inside iframes, even when the iframe is using a cross-domain source. This was only possible with Firefox and IE previously. In Chrome, I was unable to test payment forms where the card fields were embedded in their individual iframes hosted by the payment service provider. Same origin worked though but only when using CSS locators.