2.2 WebDriver Locators
Webdriver reads the html source code to locate the elements we need to interact with while automating. A typical HTML source code consists of several tags (div, h1, form, span etc.), the tags have several attributes (class, id, name, type etc.). Webdriver makes use of this tag-attribute structure to find the desired elements and perform actions on them. HTML code looks as below.
<div class="wrapper">
<h2>Sample Form</h2>
<form action="#">
<input name="text" type="text" placeholder="Enter name" />
<input name="password" type="password" placeholder="Enter password" />
<button id="submitBtn">Submit</button>
</form>
</div>We can locate elements in following ways
driver.findElement(By.id("submitBtn"));
driver.findElement(By.name("username"));
driver.findElement(By.linkText("Link to google"));
driver.findElement(By.className("form"));
driver.findElement(By.cssSelector(".form input"));
driver.findElement(By.xpath("//input[@name='password']"));Follow the below sequence while choosing the method of choosing element. Choose the one that appears higher in the sequence. Choose name only if there is a lot of decision making to be done for complex user interface.
id
xpath
class
linkText (if link)
cssSelector
name
Create a new package called code and add a class named SampleScript in it. Copy the below code into editor. Run the script and verify it works.
Last updated
Was this helpful?