Selenium Element Selectors

To perform operations and tasks on webpage and or elements of the page, the selenium script requires to be aware of the element on which the operation is to be performed. Selenium provides a range of element selection method for that purpose. We will be looking at the most commonly used ones.


Selenium generally selects elements in a very similar way that HTML provides through CSS and JavaScript. For example elements can be selected through their “id” or their “className” or simply their names. WebDriver uses various method provided byt the By class like Below are some selectors that are used by WebDriver for selecting the elements.


    Using this method elements are selected by their id.

    WebElement element = driver.findElement("submit"));;
  • By.tagName()

    Using this method elements are selected by the element’s tag name.

    WebElement element = driver.findElement(By.tagName("button"));;
  • By.className()
    Using this method elements are selected by their className attribute. Notice that this method is used with the “driver.findElements()” as the “className()” method is expected to retur a list of webElements and to access each element driver has to iterate through the list.

    List  nameElements = driver.findElements(By.className("first-name"));
    Iterator  nameIterator = nameElements.iterator();
    while (nameIterator.hasNext()) {
    Using this method elements are selected by their name.

    WebElement element = driver.findElement("q"));
    element.sentKeys("Some Text");
  • By.linkText()
    Using this method link elements are selected by matching the visible text of the element.

    WebElement element = driver.findElement(By.linkText("Selenium Learning"));;
  • By.partialLinkText()
    Selecting a link element by matching partial visible text of the element.

    WebElement element = driver.findElement(By.partialLinkText("Selenium"));;
  • By.css()
    Selecting element by css selection method

    WebElement element = driver.findElement(By.partialLinkText("#main-content div.heading"));
  • By.xpath()
    Selecting element by the xxapth of the element. This method is not very promising since the x path of some element can change is the web page adds elements dynamically and its working differs on different browsers.

    List  elements = driver.findElements(By.xpath("//*[@id="side"]/div[2]/div/ul/li/ul/li[3]/a"));
  • JavaScriptExecutor
    Selenium provides a JavaScript way to do things. The Selenium API provides a JavaScriptExecutor wrapper to execute This method doesn’t care about the current state of the target element. No matter if the element is at the bottom of the page and requires scroll to access it. So this method is a bit hackish since from the user point of view you cannot click(or interact with) an element if the element is present at the bottom of the page and requires some scrolling. In this method we use JavaScript to perform the click. This method will succeed as long as the element is present in the page. The “arguments[0]” represents the element to be used by JavaScript.

    WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#control')[0]");
  • Selection Windows/Frames
    Sometimes a test may need to open up a new window or frame and to continue with operations of them requires the WebDriver to be told to switch between them. Selenium provides API for that and below is an example of it.


    If the webpage has an iframe embedded into it by default the scripts executes on the main window. Below is how to execute script in the frame


    To switch back to main window following code can be used

  • Handeling alerts/confirmation popups
    To handle alert or confirmation boxes the switchTo() method can be used. Below is an example for accepting(confirming i.e., clicking on “OK”) a confirmation/alert popup.

    Alert alert = driver.switchTo().alert();

    To reject a confirmation box(i.e., clicking “Cancel” or “NO”)

    Alert alert = driver.switchTo().alert();

Selectors above are the most commonly used ones and are enough to perform action on a well structured web page.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s