Locating Strategy by Name

In this section, we will discuss on how to handle Locating Strategy by Name in selenium webDriver.

  Name: <input type="text" name="name"><br>
  Email: <input type="text" name="email"><br>
  <input type="submit" value="Submit">

From the above code, you can notice that the attribute “name” is attached to <input> tag. That means you can use this name attribute to find the element of the web page. This strategy following the Attribute-Based locator strategy. Let’s understand the below code.

WebElement wb = driver.findElement(By.name(name))

Here findElement will search the name attribute whose value is “name” and “email” and return and WebElement object variable.

Scenario with Example

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver;   
public class Locator_By_ID {   
public static void main(String[] args) 
WebDriver driver= new ChromeDriver() ; 

Leave a Reply

Your email address will not be published. Required fields are marked *