Tuesday 11 October 2016

Selenium APIs To Locate Elements

In our previous tutorials we have seen that how to inspect the web page to get the HTML code. On the basis of this HTML code we have also seen that how to find XPATH to locate elements. Apart from Xpath there are different ways to locate an element using APIs provided by selenium tool. 

In our test case we are going to write the series of steps which we want to automate like clicking on a button and entering text in textbox.To do so we need to find out on which button we want to click and pass it on to Selenium API.

Le us see Most commonly used APIs with simple examples.

1. Find By ID

HTML = <button id="submit1" class="cname" > Submit </button>

Selenium = WebElement submitbutton= driver.findElement(By.id("submit1"));

2. Find by Link

HTML = <a href="http://www.qaautomated.com" > Click Here </a>

Selenium = WebElement linkText= driver.findElement(By.linkText("Click Here"));

3. Find by Class Name

HTML = <button id="submit1" class="cname" > Submit </button>

Selenium = WebElement submitButton= driver.findElement(By.class("cname"));

4. Find by Partial Link

HTML = <a href="http://www.qaautomated.com" > Click Here </a>

Selenium = WebElement linkText= driver.findElement(By.partialLinkText("Click"));

5. Find by Xpath

HTML = <button id="submit1" class="cname" > Submit </button>

Selenium = WebElement submitbutton= driver.findElement(By.xpath("//button[@text()='Submit']"));


In this way you can use the HTML code associated with Web Element to locate in your selenium test case so that you can perform actions on them lick click, type text and double click etc.

I hope this post helps you . Please share your feedback and questions in comments section below. This gives me motivation to write more and more.

Video Tutorial - 



0 comments:

Post a Comment