8

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.

eebbesen
  • 5,070
  • 8
  • 48
  • 70
chucknor
  • 837
  • 2
  • 18
  • 33
  • just want to confirm this does your element change dynamically?? clear your cache and check the element if it changes its xpath.. – The Rookie Jul 17 '13 at 05:33

5 Answers5

12

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();
Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
  • Thanks Mark. I am still having issues though. I used <<>> and got org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"aspnetForm"}. – chucknor Jul 16 '13 at 16:22
  • Could you possibly provide a html snippet for the form and its surrounding tags to make sure. Thanks. – Mark Rowlands Jul 17 '13 at 07:11
  • Sure. Hope this is what you need ..
    – chucknor Jul 17 '13 at 07:25
  • The url is http://www.royalmail.com/postcode-finder and it's the Find a postcode form. – chucknor Jul 17 '13 at 07:26
  • Ah yes, at the top of the page there is an `iframe` element that bounds the form you're wanting to interact with. Try `driver.switchTo().frame(driver.findElement(By.id("pca-iframe"));` Some people also like to assign the frame to a new instance of a `webDriver` to keep things neat but I think that's personal preference. – Mark Rowlands Jul 17 '13 at 07:32
  • Brilliant!! Thanks Mark. That's better. – chucknor Jul 17 '13 at 07:41
0

Check your xpath . try to use simple

 driver.switchTo().frame(1);

with wait statement.

0

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

olyv
  • 3,699
  • 5
  • 37
  • 67
jyothi
  • 1
0

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

Pete
  • 57,112
  • 28
  • 117
  • 166
jyothi
  • 1
0

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

driver.swithcTo().frame();

abhiman
  • 11
  • You could improve your answer by adding further explanation, code markup and documentation links. – dakab Sep 01 '15 at 17:01