org.openqa.selenium.NoSuchElementException: Unable to locate element

770 views
Skip to first unread message

Imran Asif

unread,
Nov 5, 2015, 4:49:27 PM11/5/15
to webdriver
Hello All:

I am trying to enter text in a field but getting the error: org.openqa.selenium.NoSuchElementException: Unable to locate element

Here is my code:
           
                String myframe;
List<WebElement> frames = driver.findElements(By.tagName("iframe"));
System.out.println("No. of frames = " + frames.size());
for(int i=0;i<=frames.size();i++){
myframe = frames.get(0).getAttribute("id");
System.out.println(myframe);
//myframe = frames.get(0).getAttribute("name");
//System.out.println(myframe);
driver.switchTo().frame(myframe);
driver.findElement(By.xpath("//*[@id='document.documentHeader.documentDescription']")).sendKeys("test");
}
//driver.findElement(By.cssSelector("input#document.documentHeader.documentDescription")).sendKeys("test");
//driver.findElement(By.xpath("//*[@id='document.documentHeader.documentDescription']")).sendKeys("test");


Here is the HTML code:
<input type="text" title="* A free-form text field that describes the purpose or function of the document." style=" " id="document.documentHeader.documentDescription" onblur="" onchange="" value="" tabindex="1" size="45" maxlength="255" name="document.documentHeader.documentDescription">

Please help.  Thanks in advance.

Adam

darrell

unread,
Nov 6, 2015, 1:16:19 PM11/6/15
to webdriver
You didn't say which line is failing. It appears your code is finding out how many IFRAME tags are on the main page, default frame. Then you try to switch to each frame and look for an element and send "test" to the element.

I haven't work with frames for years now but I'd wonder if you can switch to frames the way you are doing it. If I am on the default/main frame then I switch to the first IFRAME, before I switch to the second IFRAME I believe I have to switch back to the default then switch to the next IFRAME. So that might be one problem with your code.

The other problem is that you are switching to the first IFRAME then trying to find the element with XPath "//*[@id='document.documentHeader.documentDescription']". If this element is not on the first IFRAME it will fail with NoSuchElementException. If this element does not exist on ALL the IFRAME elements then the first IFRAME which does not have this element will cause the exception.

In other words your code is saying:

for each iframe {
switch to the iframe
send the text "test" the web element with XPath "//*[@id='document.documentHeader.documentDescription']"
}
   
Is that what you want to do? Or are you trying to write:

for each iframe {
switch to the iframe
if the web element with XPath "//*[@id='document.documentHeader.documentDescription']" exists {
send the text "test" to it
}
}

These are two VERY different algorithms. The first assumes that the web element with XPath "//*[@id='document.documentHeader.documentDescription']" exists on all iframes. The second only sends the text IF the element exists on that iframe.

sunaina jain

unread,
Nov 17, 2015, 12:43:29 PM11/17/15
to webdriver
Hi
Anyone knows how can I automate UI testing on Mobile interface through Chrome developer tools. 
Any ideas?

Louise

unread,
Nov 17, 2015, 12:43:29 PM11/17/15
to webdriver
Also, are the iframes nested? Otherwise you need to switch back to the parent frame before switching to next frame.

For example use driver.switchTo().defaultContent() or driver.switchTo().parentFrame() at the end of the for loop. 
Reply all
Reply to author
Forward
0 new messages