...

/

Locating Web Elements

Locating Web Elements

Discover what you can do with locators, and try it out yourself.

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

...