top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Selenium WebElement Commands

Selenium is an open source implementation tool used for automating web applications, and it provides a wide range of methods and functions to interact with web elements. A WebElement in Selenium is essentially an HTML element on a website.


Selenium Method Categories


Selenium offers lot of methods to interact with web elements. These methods are typically available through the webdriver object once you've created an instance of a web driver, such as ChromeDriver or FirefoxDriver.


There are total of 5 Selenium Method Categories. The categories are Browser Methods, WebElement Methods, Navigation Methods, Wait Methods, and Switch Methods. Each category has a group of methods that perform actions via Selenium:


  • Browser Methods - perform actions on a browser.

  • WebElement Methods - perform actions on WebElements.

  • Navigation Methods - load a web page, refresh a web page, or move backwards and forwards in our browser’s history.

  • Wait Methods - pause between execution statements.

  • Switch Methods - switch to alerts (pop-up), windows, and frames.

List of Selenium WebElement Commands


In this blog, we focus on the following web element methods that carry out actions on the web page via WebElement Interface.


1) findElement()


The findElement() method is an important WebElement method. It’s important because we have to first find the WebElement before performing an action on the WebElement. If there are multiple elements with the same locator value then findElement() locates the first WebElement. Therefore, it’s best to find an element by a unique locator value. Once located, you can perform various actions on the found element, such as clicking on it, inputting text, or extracting information.

Syntax:

  • findElement(by, value): Finds the first web element matching the specified locator strategy (e.g., By.ID, By.XPATH, By.CSS_SELECTOR).

  • findElements(by, value): Finds all web elements matching the specified locator strategy.



2) click() command


In Selenium, the click() method is used to simulate a click action on a web element, such as a button, link, or any other clickable element on a web page. This action is often used to interact with web applications and automate tasks like submitting forms, navigating between pages, or triggering certain actions on a website.

Syntax:

  1. Locate the WebElement: First, you need to locate the web element you want to click on. You can do this using various locators such as ID, name, CSS selector, XPath, etc. You typically use methods like find_element_by_* or find_elements_by_* (e.g., find_element_by_id, find_element_by_xpath) to locate the element.

  2. Perform the click action: Once you have located the element, you can call the click() method on it to simulate a click event.


3) sendKeys() command


The sendKeys method is a commonly used function in Selenium, which is a popular automation testing framework for web applications. This method is used to simulate keyboard input and send keystrokes to web elements, such as input fields, text areas, or any other element that accepts user input. It allows you to interact with these elements programmatically as if a user were typing on a physical keyboard.

Syntax:

element.sendKeys("text to type");



4) getText() command


This method is used to retrieve the text content of a web element on a web page. This method is particularly useful when you want to extract text from elements such as headings, paragraphs, links, buttons, or any other HTML element that contains visible text.

Syntax:

element.getText();



5) getAttribute() command


This command retrieves the attribute value of a specified element. It uses String as the parameter and returns a string value as its result.

Syntax:

element.getAttribute();


6) clear() command


This method is used to clear the text from an input field or textarea element. This method is typically used when you want to remove the existing text from an input field before entering new text into it.

Syntax:

element.clear();



7) isDisplayed() command


This method is used to check if a web element (such as a button, link, text field, etc.) is currently visible or displayed on a web page. It returns a Boolean value, either true if the element is displayed or false if it is not displayed.

Syntax:

element.isDisplayed();



8) isEnabled() command


This method is used to check whether a web element (such as a button, input field, or link) is currently enabled or not on a web page. An element is considered enabled if it can be interacted with (e.g., clicked, typed into, etc.), and it is not disabled or hidden.

Syntax:

element.isEnabled();


9) getLocation() command


This method is used to retrieve the location (X and Y coordinates) of a web element on a webpage. This method returns a Point object that contains the X and Y coordinates of the element's location.

Syntax:

element.getLocation();



10) isSelected() command


This method is primarily used to check whether a web element, such as a checkbox or a radio button, is selected (checked) or not. It returns a boolean value, true if the element is selected and false if it is not selected.

Syntax:

element.isSelected();


11) submit() command

This method is used to submit a form element, typically a <form> element, on a web page. When you call the submit() method on a WebElement, Selenium will simulate clicking the submit button of the form, or pressing the Enter key if the focused element is an input field inside the form.

Syntax:

element.submit();



The selenium commands mentioned above are all essential for testers seeking to automate user actions on a website to verify its performance.

Thanks for Reading….


206 views0 comments

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page