0

I want to find the input element in the iframe, however selenium is not able to locate it.

I used both switchTo().frame(id) and switchTo().frame(index), but none of it works.

Here is my code:

driver.switchTo().defaultContent();
WebElement ele = driver.findElement(By.xpath("//iframe[contains(id, 'braintree-hosted-field-number')]"));
driver.switchTo().frame(ele);

Error Message:

no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[contains(id, 'braintree-hosted-field-number')]"} (Session info: chrome=76.0.3809.132)

Screenshot:

Application HTML

frianH
  • 7,295
  • 6
  • 20
  • 45
Jed Feng
  • 1
  • 1
  • 1
  • change id to @id or just . – pguardiario Aug 28 '19 at 03:33
  • you should specify the attribute `id` as `@id` in your xpath. If still the iframe not detected then check if it's the another parent iframe by any chance. Btw, it's not a good idea to post the html of screenshot. – supputuri Aug 28 '19 at 03:35
  • @supputuri sorry, I am pretty new for posting, how should I post the html? Also I used @ id and still not detecting the frame. – Jed Feng Aug 28 '19 at 03:55

4 Answers4

3

Try use WebDriverWait with frameToBeAvailableAndSwitchToIt, like this:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("braintree-hosted-field-number")));

Following import :

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
frianH
  • 7,295
  • 6
  • 20
  • 45
0

In iframes we can switch with name, id or index

driver.switchTo().frame("name or ID");    
driver.switchTo().frame(index);

One frame- Directly switch to the frame (with the above code)

Multiple frame - Switch to the exact frame

Nested frame - Switch to the parent frame (parent frame) and then switch to the child frame(inner frame)

driver.switchTo().frame("parent frame");
driver.switchTo().frame("child frame"); 

Switch to frame with ID or Name If you have only one iframe then directly switch to the frame otherwise switch to parent frame and then point to the child frame.

driver.switchTo().frame("braintree-hosted-field-number");

Switch to frame with index Seeing your html it looks like there is one iframe and then the index starts with "0". Example: If you have 5 iframes then the index will be 0 1 2 3 4

driver.switchTo().frame(0);
Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
0

Seems you were close. To switch to the desired <iframe> you need to:

Here you can find a detailed discussion on Ways to deal with #document under iframe

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

By using https://github.com/nick318/FindElementInFrames you can delegate this manual iframe switching and just search the element you need.

SearchByFrames searchInFrame = searchFactory.search(() -> driver.findElement(By.tagName("body")));
Optional<WebElement> elem = searchInFrame.getElem();

It will look for your element across all available iframes, it works stable and fast.

nick318
  • 575
  • 4
  • 18