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.

1
  • 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, 2013 at 5:33

5 Answers 5

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();
6
  • Thanks Mark. I am still having issues though. I used <<<driver.switchTo().frame(driver.findElement(By.xpath(".//*[@id='aspnetForm']/div[3]")));>>> and got org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"aspnetForm"}.
    – chucknor
    Jul 16, 2013 at 16:22
  • Could you possibly provide a html snippet for the form and its surrounding tags to make sure. Thanks. Jul 17, 2013 at 7:11
  • Sure. Hope this is what you need ..</title></head> <body> <form name="aspnetForm" method="post" action="postcodefinder.aspx" id="aspnetForm"> <div> <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
    – chucknor
    Jul 17, 2013 at 7:25
  • The url is royalmail.com/postcode-finder and it's the Find a postcode form.
    – chucknor
    Jul 17, 2013 at 7: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. Jul 17, 2013 at 7:32
0

Check your xpath . try to use simple

 driver.switchTo().frame(1);

with wait statement.

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

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

0

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

driver.swithcTo().frame();

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.