Locators
Mechanism to find elements in a web page
There are 7 ways which we can use to find an element or group of elements in a web page. And they are
Id
Name
Tag name
Link text
Partial link text
CSS Selector
XPath
Id:
If an element has id
attribute then we can use it find the element.
WebElement element = driver.findElement(By.id("id-value"));
Name:
If an element has name
attribute then we can use it find the element.
WebElement element = driver.findElement(By.name("name-value"));
Tag name:
We can find an element using it'stagname
.
WebElement element = driver.findElement(By.tagName("a"));
Link text:
If an element is having tag a
, then we can identify it using the link text of it.
WebElement element = driver.findElement(By.linkText("link-text-value"));
// For example in Goole search page we can find the gmail link element as
WebElement element = driver.findElement(By.linkText("Gmail"));
Partial link text:
If an element is having tag a
, then we can identify it using a portion of it's text.
WebElement element = driver.findElement(
By.partialLinkText("partial-value-of-link-text"));
// For example in Goole search page we can find the gmail link element as
WebElement element = driver.findElement(By.partialLinkText("mail"));
Last updated
Was this helpful?