chucknor
chucknor

Reputation: 849

Selenium WebDriver cannot locate element within an iframe, and throws NoSuchElementException

I realise there are several queries on here for this same problem but none of them provide a solution to my particular problem.

I am running a web driver test that tries to fill out a form for a mail website to find postcodes based on address details. I keep getting this error when trying to locate the first text box:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#ctl00_BodyContent_txtBuildingNumber"}

I have used xpath and the id to try and locate the element but I keep getting the error. I can see that this element is present when the webdriver is running and I have been able to locate another text element on the page and enter text, but I keep getting this error for this field and other fields within the frame.

I am guessing that the problem must be to do with the fact that this field is part of an iFrame.

I have used implicit waits within the test but with no success. I still get the error.

Upvotes: 8

Views: 23482

Answers (5)

abhiman
abhiman

Reputation: 11

you can use wait statement and after using the wait statement use simply

driver.swithcTo().frame();

Upvotes: 0

jyothi
jyothi

Reputation: 1

The following worked for me

driver.switchTo().frame(myd.findElement(By.xpath("//*[@id='page-15']/div/p/iframe")));

//*[@id='page-15']/div/p/iframe is the xpath of the frame in which the button I was trying to click.(driver is of type WebDriver i.e WebDriver driver) thank you

Upvotes: 0

jyothi
jyothi

Reputation: 1

sorry a little correction the following worked for me

driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='page-15']/div/p/iframe")));

//*[@id='page-15']/div/p/iframe is the xpath of the frame in which the button i was trying to click was located. (driver is of type WebDriver i.e WebDriver driver ) thank you

Upvotes: 0

Mark Rowlands
Mark Rowlands

Reputation: 5453

By the sounds of it you'll need to first switch to the iframe element that contains the element that you want to interact with. (Although without seeing the relevant HTML this is a bit of an extrapolated guess).

driver.switchTo().frame();

eg:

driver.switchTo().frame(1);
driver.switchTo().frame(driver.findElement(By.id("id")));

When you've finished interacting with the elements within the frame, you'll need to switch back to the main webpage.

driver.switchTo().defaultContent();

Upvotes: 12

Archana Singh
Archana Singh

Reputation: 39

Check your xpath . try to use simple

 driver.switchTo().frame(1);

with wait statement.

Upvotes: 0

Related Questions