Search⌘ K
AI Features

Locating Web Elements

Explore how to locate web elements in Selenium WebDriver using various selectors including ID, name, link text, partial link text, XPath, tag name, class name, and CSS. This lesson helps you understand and apply these locators to automate web interactions effectively in Node.js.

Find element by ID

Using the IDs is the easiest and safest way to locate an element in the HTML. If a web page is W3C HTML conformed, the IDs should be unique and identified in the web controls of the page.

In contrast to texts, IDs are less prone to change (e.g., developers may decide to change the label but are less likely to change the ID). We can do this by completing the following command.

driver.findElement(webdriver.By.id("submit_btn")).click();

Find element by Name

The name attributes are used in the form controls (like text fields and radio buttons, etc.) The values of these name attributes are passed to the server when a form is submitted. Similar to ID, the name attribute is less likely to change. We can find element by name by:

driver.findElement(By.name("comment")).sendKeys("Selenium Cool");

Find element by Link Text

The most ...