WebDriver is not able to find out WebElement present on Web Application

Recently came across one of the problem and I think it would be helpful to everyone if I share the details.

Problem Statement: WebDriver was not able to find out WebElement

I was using Firefox WebDriver, and in one of my test WebDriver was not able to find out Link / Image / Button for some reason.

HTML page Code look like below: I want to click on "New" link/image.

<div id="toolcontainer">
       <ul id="toollist">
              <li>
                     <a id="btnNewDash" title="New" onclick="javascript:$('#addTab').dialog('open'); return false;" href="#">
                            <span class="toollistimg"></span>
                            <span class="toollisttext"></span>
                        </a>
                 </li>
              <li></li>
          </ul>
   </div>

But when I use below code in my test, it didn't work... I tried following -

driver.findElement(By.cssSelector("span.toollisttext")).click();
driver.findElement(By.className("toollisttext")).click();
driver.findElement(By.xpath("//a/span[@class='toollisttext']")).click();
driver.findElement(By.xpath("css=span.toollisttext")).click();


I also tried using hyper links:
driver.findElement(By.id("btnNewDash")).click();
driver.findElement(By.cssSelector("a#btnNewDash")).click();


None of the above mentioned options worked.

I was not able to figure out the problem... but then one of my friend realized that the particular link was actually under iFrame and if any webElement is in iFrame, we have to first focus the iFrame and then only we can perform actions on any WebElements present in iFrame.

We changed the code and it started working.

// switch your focus to iFrame first
driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='Framework_Business_0']/iframe")));
// click on Webelement present in iFrame
driver.findElement(By.xpath("//*[@id='btnNewDash']")).click();


Solutions / Debug :
  1. Find out first whether you are using correct locator, whether its ID, Name, CSS or ClassName is correct
  2. If it is correct then check if problem is because of page loading.. like WebDriver can't find out WebElement if page is still loading. If this is the case then perform WaitOnPageLoad operation or Wait till that web element is visible on page and then perform operation on that WebElement.
  3. Sometime few web element gets populated on run time, check if that is not the case. If that is the case then first perform operation on web by which that dynamic web element will get visible.
  4. If you tried all above options and still it is not working then check if that web element is in iFrame or not. It that web element is in iFrame then first you have to focus iFrame and then only you can able to click on Web elements present in that iFrame.
In our case, it was failing because of the reason mentioned in 4th point above.

I hope this helps!

Keep visiting my blog. Thanks! 
Related Posts Plugin for WordPress, Blogger...
Powered by Blogger