Tuesday, 28 October 2014

How to handle google auto capture


 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class GoogleAutoCap {  
  public static void main(String[] args) throws InterruptedException {  
  // TODO Auto-generated method stub  
  WebDriver driver = new FirefoxDriver();  
  driver.get("http://www.google.com");  
  driver.findElement(By.id("gbqfq")).sendKeys("selenium");  // Entering values into the search box
  Thread.sleep(2000);  
  List<WebElement> rows = driver.findElements(By.xpath("//*[@class='gssb_m']"));  // Finding all elements under tag td for auto suggestions
  for (int i=0;i<rows.size();i++)  
  {  
   List<WebElement> cols = rows.get(i).findElements(By.tagName("span")); // span tag contains the result   
   System.out.println(cols.size());  
   for (int j=0;j<cols.size();j++)  
   {  
   System.out.println(cols.get(j).getText()); // printing them one by one 
   }  
  }  
  }  
 }  

Tuesday, 21 October 2014

How to work with similar elements on the page

There are many instances when a developer re-uses a code in  a webpage. For example "OK" button for a prompt. Instead of scripting it again and again, developers prefer re-using a code which results in presence of similar elements on the page .

By similar elements I mean - Elements with same ID and properties;
for example
<span id="OkButton" type="button" value="OK"/>

Let's assume you have 4 copies of the above mentioned element on the page and you want to work with the third element on the same page .

NOTE- Elements appear on a page in order of Top to Bottom . So the third element means from top to bottom i.e third time we spot the same element.

To solve our problem xpath comes in handy for us

First Element
//span[@id='OkButton'] -- will give you first element from top

Second Element
//span[@id='OkButton']//following::span[@id='OkButton'] -- will give you second element on page from top

Third Element
//span[@id='OkButton']//following::span[@id='OkButton']//following::span[@id='OkButton'] -- thrid element

Fourth Element
//span[@id='OkButton']//following::span[@id='OkButton']//following::span[@id='OkButton']//following::span[@id='OkButton'] -- fourth element


NOTE- Some people suggest indexing here i.e. //span[@id='OkButton'][3] -- to move the third element -- This works only in the case when all the similar elements are present just below each other 

<span id="OkButton" type="button" value="OK"/>
<span id="OkButton" type="button" value="OK"/>
<span id="OkButton" type="button" value="OK"/>
<span id="OkButton" type="button" value="OK"/>

And this situation is very rare . As if we consider our example then every OK button will be placed under its specific set of code . 

Wednesday, 1 October 2014

How to Diagnose Element not present on page

The most common error faced by  beginners is "Element not found" . There are multiple reasons for this error to occur, so there are multiple solutions for the problem. Below are some of the common diagnosing techniques I learned with time and would like to share with you people. These techniques could be helpful or may be not, depending on your problem.

" You have used the correct id/xpath and when you inspect the element, Its visible. But , on runtime you are facing the error. "

Solutions - Check that your id/xpath should not result in 2 elements while inspecting element. Normally, what happens that developer re-uses the same code for some elements if the functionality is repeated . This results in duplicate value in a page. So, Inspecting element may highlight the element you want  but  in code that element falls under different category and will result in error. Move to the duplicate element using indexing or following commands and see if it helps you.

 - Use .isDisplayed() and .isEnabled() methods on the element. This will result in true or false. If one them results in false then you have to diagnose ahead. Sometimes, the element is present while inspecting, in the code as well but not visible on the screen. In those cases .isDisplayed() results false. Scroll your page and make the element visible on page and then try working on it. If .isEnabled is false then the element is hidden. You have to first diagnose that how and why this happens, Contact your developer in this case and ask him to correct if its a problem in his code.

- Add some wait before accessing the element. This scenario is usually for the ajax elements. The elements may vary on loading time according to your connection and will result in error. Use Explicit wait or normal Thread.sleep();

- Element is visible on the screen but present under the Iframe and not inspectable. Iframes are concept of page under a page, They are the part of the main code but to access element present inside iframe needs switching to an iframe. Switch and use id/xpath to inspect the element.


Post comments below if these solutions helped you in some way or even solved your problem. If not, lets try working together to find a solution :)