CHAPTER 2

image

Locating Web Elements

As you might have already figured out, to drive an element in a page, we need to find it first. Selenium uses what are called locators to find and match the elements on a web page. There are eight locators in Selenium, as listed in Table 2-1.

Table 2-1. Locators in Selenium

Locator

Example

ID

FindElement(By.Id("user"))

Name

FindElement(By.Name("username"))

Link Text

FindElement(By.LinkText("Login"))

Partial Link Text

FindElement(By.PartialLinkText("Next"))

XPath

FindElement(By.Xpath("//div[@id="login"]/input"))

Tag Name

FindElement(By.TagName("body"))

Class Name

FindElement(By.ClassName("table"))

CSS

FindElement(By.CssSelector, ...

Get Selenium WebDriver Recipes in C#, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.