Nearly a year back I started using selenium webDriver to automate certain processes in testing  which would otherwise take my time to run same steps over and over again.

Below mentioned are few concepts which I found useful when I encountered certain cases some of which were a bit complex initially being new to selenium.

Let me use example cases to explain these concepts.  Please note, the following code snippets are of selenium using java.

Case 1

There is a webpage where you need to select an webElement. Selecting a webElement should be such that with minimum filters we can find the required element. Try to find the unique attribute which can help to identify the element ie either

  1. Id - driver.findElement("nav01"));
  2. Classname -  driver.findElement(By.classname("navBar"));

Usually these 2 give the required element in one go.

But at times one may also need some additional filtering before reaching to the required element. Ie first selecting the section with the unique parameter say class

  • WebElement mainTab = driver.findElement(By.classname(“navLvl2"));

Next in selected html area(mainTab) search for the required element

  • WebElement subTab = mainTab.findElement(By.tagname(“li"));

Both the above can statements can be compiled into one using cssSelector as follows

  • WebElement subTab = driver.findElement(By.cssSelector("li[class = 'navLvl2']"));

Case 2:    

Selecting list of elements (say from menu bar) and iterating over each element to find a particular element before clicking over it.

Find the menuBar area

  • WebElement MenuBar = driver.findElement(By.cssSelector("div[class = 'menu01']"));

Under this element find the list of elements which constitute the buttons/tabs of menuBar

  • List<WebElement> Tabs = MenuBar.findElements(By.tagName("li"));

Now if you need specific tab/button to be clicked upon, then decide upon the criteria as available to detect the element. I will select the text as the criteria for selecting the element.

for(WebElement Tab: Tabs) 

Case 3:    

If the webpage displays an ad on the page and one needs to access an element hidden behind the ad view area then user has 2 options.

 a) Either add a wait command, to allow the program to wait until the ad/pop up displays and goes after its set period.

         To do this user will need to add a wait statement as follows

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class  = 'entrance-title']")));

         This will make the program wait for 60 secs for the required element to get visible in the view area.    

         The selection criteria in By.cssSelector() can be anyone which allows you to uniquely identify the  element.

 b) Remove the blocking element from the view: One can detect the pop up/ad which is being  displayed over the required element.

Example: There is an introduction video being played over a webpage. 1st one needs to detect the element holding the video panel in this case. And next remove it.

// Here we detect the video panel
if (driver.findElements(By.cssSelector("div[id = 'introVideo']")).size() > 0) 
    // Remove panel
    ((JavascriptExecutor) driver).executeScript("return document.getElementById('introVideo').remove();");  

This seemingly is a better approach than using wait as waitTime adds to programs total completion time. Also one cannot be sure of how long to wait as depending upon the internet speed at a given time, if slow the element might take longer time to completely display and to go invisible/hide.

Case 4:   

When the required element is not in the viewport (in the browsers viewable area).

This case occurs when browser has moved page up/down depending upon your previous actions and the required element has also moved out of the browsers viewable area.

To get the element back into browsers visible area.

Add the following statement in case your element is at the top of the page.

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript("scroll(0, -250);");

The co-ordinates mentioned within the scroll function can differ depending upon the location of the required element on the page.

One can also use the Actions along with moveToelement function as follows

Actions action = new Actions(driver);


If the required Element is significantly out of view, then after the moveToElement statement , add a thread.sleep(2000) to provide the webpage some time to travel till the required element. The time limit in sleep function can be any amount as per the requirement.

This is to prevent the next action from happening while the browser is traversing the webpage, which might then throw an error as element not clickable/ not visible at the given location.

I hope these few cases help you out.