Selenium-WebDriver: Most commonly Used WebElement Methods.

WebElement methods are used for interacting with elements on a website, such as clicking a link or button, activating an input field, opening a dropdown menu, etc. WebElement methods are accessed by calling the appropriate method from the WebElement class and passing in the elements you want to interact with.

This blog helps you to start interacting with elements available on the web page using Selenium WebDriver, Below mentioned methods are the most commonly used WebElement methods, and their demo examples with java Selenium-WebDriver code snippets.

  • findElement(By)
  • findElements(By)
  • sendKeys
  • clear()
  • click()
  • getText()
  • isDisplayed()
  • isEnabled()
  • isSelected()
  • getAttribute()
  • getTagName()
  • getSize()
  • submit()
  • getLocation()
  • getCssValue​()
  • getRect()
    Here, In this blog, we will explore the most commonly used methods. The remaining web element methods will be covered in upcoming blogs according to the order given above in the types mentioned.

FindElement(By or by arg[]) Method:

The method findElement (By or by)  is used for locating the first matching web element on a web page as per different locator types used and it will find a single web element as per the value passed in it.

Syntax:

WebElement : findElement (By or by );
(By.LocatorType with web element value from DOM)
Here, WebElement represents a return type, which locates the single HTML element on the website page. The findElement() represents the method name, and By represents the abstract class within which all the locators are defined, and using it we can locate the web element with the help of its value. For more ref findElement(By).

Code Snippet:

driver.findElement(By.cssSelector("textarea[id ='description']"));
//or
WebElement descriptionBox = driver.findElement(By.cssSelector("textarea[id ='description']"));

In the above code snippet, using the driver object we can call the findElement method by using different locator types to locate a web element on a web page. To know about locators type please refer to our previous blog on locators. For ref - link1, link2, link3.

FindElements(By or by arg[]) Method:

The method findElements(By by)  is used for locating more than one web element on a web page using the locator’s type. Using this method, we find multiple web elements with a common value.

Syntax:

List<WebElement> : findElements(By or by);
Here, In this case, List<WebElement> represents a return type that finds multiple HTML elements on a webpage, findElements() represents a method name, and By represents an abstract class in which all locators are defined, and we can use them to locate web elements by using their values.

Code Snippet:

driver.findElements(By.tagName("a"));
//or
List <WebElement> links = driver.findElements(By.tagName("a"));

In the code snippet, using a driver object, we can call the findElements method in code and we can also locate web elements on web pages by using different locator types. For more ref findElements(By).

SendKeys() Method:

The sendKeys() allows passing or entering String type values in a text box or edit box on the web page, so we can simulate by typing in any type of web element available on the webpage.

Syntax:

Void : sendKeys();
Here, void represents return type, sendKeys() represents, method name and in sendKeys() we usually pass a String value.

Code Snippet:

driver.findElement(By.id("name")).sendKeys("Testing_01");

In code Snippet, using the driver object we can call the findElement method. When we used the findElement method and defined it in class/code then we were able to call the sendKeys() method. As a result of calling findElement(), sendKeys() can be accessed. For more ref sendKeys.

Clear() Method:

The clear() method is used for clearing or removing all passed values in the edit/text box. Using this method, we can remove the value from the web element if a web element accepts string values.

Syntax:

Void : clear();
Here, void represents return type, clear() represents method name, It is mostly used for text boxes.

Code Snippet:

driver.findElement(By.id("name")).clear();

In the code Snippet, using the driver object we can call the findElement method. When we call findElement then after we were able to call the clear() method. As a result of calling findElement(), the clear() method can be accessed. For more ref clear().

Click() Method:

The click() method is used to perform click operations on web elements whether it is buttons, checkboxes, dropdowns, etc.

Syntax:

Void: click();
Here, void represents return type, and click() represents method name.

Code Snippet:

driver.findElement(By.xpath("//button[@type='submit']")).click();

In the code Snippet, using the driver object we can call the findElement method in class code. When we call findElement in selenium code then after we were able to call the click() method. As a result of findElement(), the click() method can be accessed. For more ref click().

For more clarification, In the below demo code snippet we have used the above-said web elements methods with an explanation. We have taken a reference from PLGWorks.

public class BlogWebElementPart1 {

	public static void main(String[] args) {

		//Set Property and giving path of browser path from device folder.
		System.setProperty("webdriver.chrome.driver","/Users/ganeshchavan/Documents/Drivers/chromedriver");

		//driver object declaration.
		WebDriver driver = new ChromeDriver();
        
		//Navigating to particular URL.
		driver.get("https://plgworks.com/");
        
/*findElement(By) >> this method will find/locate very first web element present on web page.
Below code locate a description text box present on plgworks domain.*/
		WebElement descriptionBox = driver.findElement(By.cssSelector("textarea[id ='description']"));
        
/*click() >> This method will perform click operation. After findElement() we can call click() method. 
Below code snippet we are clicking on 'GET IN TOUCH' button.*/

        driver.findElement(By.cssSelector("ul[class='ml-auto text-16 font-bold navbarCss_list__xPFtz'] a[title='GET IN TOUCH']")).click();

/*findElements(By ) >> This method will used to locate a multiple web element at one time. Here we are counting links present on web page.*/

		List<WebElement> links = driver.findElements(By.tagName("a"));

//printing count of total links present.
		System.out.println(links.size());

//sendKeys() >> this method used for giving String I/P in text field.

		driver.findElement(By.id("name")).sendKeys("Testing_01");
		driver.findElement(By.id("email")).sendKeys("Xyz@gmail.com");
        
/*clear() >> this method used for clearing a input given in web element like text box.*/

		driver.findElement(By.id("name")).clear();
		driver.findElement(By.id("email")).clear();

		
		
	}

}

We will be covering the remaining web element methods in the upcoming blog, stay tuned.

Ganesh Chavan

Ganesh Chavan

QA Test Engineer at PLGWorks.com