Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 481
chucknor Avatar asked Jul 16 '13 14:07

chucknor


People also ask

Will a find element type call throws NoSuchElementException when it can't find the element?

findElement method is used to access a single web element on a page. It returns the first matching element. It throws a NoSuchElementException exception when it fails to find If the element.

Which helps to verify the WebElement is active in Dom?

isDisplayed() is the method used to verify presence of a web element within the webpage.

What is the return type of driver get URL?

Get Command It accepts String as parameter and returns void. The respective command to load a new web page can be written as: driver. get(URL);


1 Answers

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();
like image 101
Mark Rowlands Avatar answered Sep 21 '22 17:09

Mark Rowlands