© Courtney Zhan, Edited by Zhimin Zhan 2024
Z. Zhan (ed.)Selenium WebDriver Recipes in C#https://doi.org/10.1007/979-8-8688-0023-8_2

2. Locating Web Elements

Courtney Zhan1  
(1)
Fitzgibbon, QLD, Australia
 
As you may have already figured out, to drive an element in a page, you need to find it first. Selenium uses locators to find and match elements on web page. There are eight locators in Selenium, shown in Table 2-1.
Table 2-1

Selenium Locators

Table

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")) ...

Get Selenium WebDriver Recipes in C#: Practical Testing Solutions for Selenium WebDriver 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.