O'Reilly logo

Selenium WebDriver Recipes in C#, Second Edition by Zhimin Zhan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required