Selenium WebElement Commands

Related Product On This Page What is a Selenium WebElement?June 05, 2026 · 9 min read · Tool Comparison

Related Product

Selenium WebElement Commands

What is a Selenium WebElement?

A WebElementin Selenium represents an HTML element on a web page, allowing interaction (click, direct keys, retrieve schoolbook) through method likeclick(), sendKeys (), and getText(). WebElements provide methods to interact with those component, retrieve information, and verify element properties.

When you use Selenium to automate a web browser, you & # 8217; ll want to name elements on the page and interact with them. Selenium WebElement objective are how you negociate those interactions. These elements are found using different locators

The WebElement interface in Selenium enable interaction with both visible and obscure elements on a web page. WebElement command return either null/void or the ingredient found.

Syntax of Selenium WebElement

& lt; start tag & gt; message & lt; /end tag & gt;
  • HTML elements can contain other elements. Every HTML document carries such HTML elements.
  • WebElement Selenium WebDriver methods hold to almost all DOM elements on a web page.
  • Each WebElement is represented inSeleniumvia the WebElement interface & # 8211; which is used by Selenium to interact with visible and invisible elements on the web page.

Every method either returns a value or returns null/void (no value). The WebElement class in Selenium WebDriver works the same way.

Here ’ s an example of a WebElement in Selenium command:

WebElement element = driver.findElement (By.id (“ UserName “));

This command returns either the element being research for or returns null/void.

All activeness on any WebElement will perpetually populate against any factor, regardless of whether an action is valid on the element or not.

List of Selenium WebElement Commands

There are different types of WebElement Commands in Selenium to interact with the web elements. These method use the Web elements to interact with both visible and obscure ingredient on a web page. Methods in Selenium either return a value or nothing (null/void).

Selenium WebElement Methods

  1. sendKeys ()& # 8211; Type text into input fields.
  2. isDisplayed ()& # 8211; Check if the element is visible on the page
  3. isSelected ()& # 8211; Check if the element is select
  4. submit()& # 8211; Submits a form or triggers a submit action on a form factor
  5. isEnabled ()& # 8211; Check if the element is enabled
  6. getLocation ()& # 8211; Returns value of the x and y coordinates of the WebElement relation to the top-left corner of the viewport.
  7. clear()& # 8211; Clear text from an input field
  8. getText()& # 8211; Get the visible text inside the WebElement.
  9. getTagName ()& # 8211; Returns the tag name of the WebElement as a string. The tag gens is the type of the HTML element.
  10. getCssValue ()& # 8211; Get the value of a specified CSS property of a WebElement.
  11. getAttribute ()& # 8211; Get the value of a specific attribute of the element
  12. click()& # 8211; Click on the WebElement
  13. getSize()& # 8211; Returns values of height and width of the WebElement.

Here is the elaborated list of WebElements Commands in Selenium:

1. sendKeys () command

allows the user to type substance automatically into an editable field while executing trial. These fields are web elements that can be identify using like element id, gens, class name, etc.

Syntax:

element.sendKeys (“ text ”);

This method uses CharSequence as a argument. It returns zero. It act with text entry elements such asINPUT and TEXTAREA.

Example:

// Create WebElement WebElement elesendKeys = driver.findElement (By.id (`` TextBox '')); // Perform sendKeys operation elesendKeys.sendKeys (`` Cheese ''); // OR // Send value to particular WebElement e.g: Textbox. driver.findElement (By.id (`` TextBox '')) .sendKeys (`` Cheese '');

Read More:

2. isDisplayed () command

The in Selenium verifies if a especial constituent is present and displayed. If the element is displayed, then the value retrovert is true. If not, then the value returned is a.

Syntax:

element.isDisplayed ();

The code below verifies if an element with the id attribute value next is displayed.

boolean eleSelected= driver.findElement (By.xpath (`` xpath '')) .isDisplayed ();

Example:

WebElement ingredient = driver.findElement (By.id (`` UserName '')); boolean status = element.isDisplayed (); //Or can be written as boolean status = driver.findElement (By.id (`` UserName '')) .isDisplayed ();

Note: If an element is present on a web page but its property is set to hidden, the Selenium WebDriver isDisplayed method will render NoSuchElementFound. This is because still though the element is present in the DOM, it is not visible to users.

3. isSelected () command

This command only works on input element such as tuner buttons, checkboxes, select options, and menu items. It is used to determine if an element is selected. If the specified element is selected, the value returned is true. If not, the value returned is false.

Syntax:

element.isSelected ();

Example:

WebElement ingredient = driver.findElement (By.id (`` Sex-Male '')); boolean status = element.isSelected (); //Or can be written as boolean staus = driver.findElement (By.id (`` Sex-Male '')) .isSelected ();

4. submit () command

This bid is handy when interact with forms (or constituent within a form) on a web page. It doesn ’ t require a parameter and return cypher.

As evident from its name, the command submit relevant info (as require) on a site. If the action triggered by this dictation alter the current web page, the method will wait until the new page loads.

Syntax:

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

element.submit ();

Example:

WebElement element = driver.findElement (By.id (`` SubmitButton '')); element.submit (); //Or can be pen as driver.findElement (By.id (`` SubmitButton '')) .submit (); WebElement element = driver.findElement (By.id (`` SubmitButton '')); element.submit (); //Or can be written as driver.findElement (By.id (`` SubmitButton '')) .submit ();

Also Read:

5. isEnabled () command

This WebElement in Selenium command verifies if an ingredient is enabled on the web page. If the element is enabled, it returns a true value. If not, it returns a false value.

Syntax:

element.isEnabled ();

The codification below verifies if an element with the id attribute value adjacent is enabled.

boolean eleEnabled= driver.findElement (By.xpath (`` xpath '')) .isEnabled ();

Example:

// Create WebElement WebElement eleEnabled = driver.findElement (By.id (`` TextBox '')); // Perform isEnabled operation eleEnabled.isEnabled (); // OR // Verify WebElement is Enabled or Not? e.g: Radio / Checkbox. driver.findElement (By.id (`` Text '')) .isEnabled ();

6. getLocation () dictation

This command retrieves the location of a specified constituent on a web page. It does not require a parameter and returns the Point object as its result. The X and Y coordinates of the element can be deduce from the Point object returned.

Syntax:

element.getLocation ();

Example:

WebElement element = driver.findElement (By.id (`` SubmitButton '')); Point point = element.getLocation (); System.out.println (`` X cordinate: `` + point.x + `` Y cordinate: `` + point.y);

7. clear () bidding

When using this WebElement in Selenium command, its value will be cleared if the element in question is a text entry. It doesn ’ t require a parameter and returns nothing.

Syntax:

element.clear ();

The open () method does not affect early web element. The text entry ingredient here areINPUT and TEXTAREA.

Example:

// Create WebElement WebElement eleClear = driver.findElement (By.id (`` TextBox '')); // Perform open operation eleClear.clear (); // OR // Clear particular WebElement e.g: Textbox. driver.findElement (By.id (`` TextBox '')) .clear ();

8. getText () bidding

This command retrieve the schoolbook within a specific web element. This includes the interior text as well as the sub-elements sans whitespace. It doesn ’ t require a argument and returns a string value. This method is often habituate to control labels, messages, error, and other elements (regard text) expose to website visitors.

Syntax:

element.getText ();

Example:

// Create WebElement WebElement elegetText = driver.findElement (By.id (`` TextBox '')); // Perform getText operation elegetText.getText (); // OR // Get text of Particular WebElement & amp; amp; Store into String driver.findElement (By.id (`` TextBox '')) .getText ();


9. getTagName () command

This method retrieve the tag name of the specified ingredient. It does not require a parameter and returns a thread value as its solvent.

Syntax:

element.getTagName ();

This bidding perform not return the value of the gens dimension. It returns the tag. For example, if the code is& lt; input name= & # 8221; foo & # 8221; / & gt;, then this command will return the tag, i.e.“input”.

Example:

// Create WebElement WebElement elegetTagName = driver.findElement (By.id (`` TextBox '')); // Perform getTagName operation elegetTagName.getTagName (); // OR // Able to get TagName of Particular WebElement & amp; amp; Store into String driver.findElement (By.id (`` TextBox '')) .getTagName (); // Create WebElement WebElement elegetTagName = driver.findElement (By.id (`` TextBox '')); // Perform getTagName operation elegetTagName.getTagName (); // OR // Able to get TagName of Particular WebElement & amp; amp; Store into String driver.findElement (By.id (`` TextBox '')) .getTagName ();


10. getCssValue () command

This dictation retrieves the CSS property value of a specified element. It does not require a argument and returns a string value as its result.

Syntax:

element.getCssValue ();
  • Color values must be returned as rgba strings. For example, if the “ background-color ” holding is set as “ immature ” in the HTML source, the value revert by the command will be “ rgba (0, 255, 0, 1) ”.
  • Shorthand CSS properties (e.g. face, ground, border, border, border-top, margin-top, padding, padding-top, outline, list-style, pause, cue) are not render, as required with DOM CSS2 specification. Access the longhand properties directly in order to access the craved values.

Example:

//Locating textBox component using CSS Selector WebElement textBox = driver.findElement (By.cssSelector (`` div # textBox ``)); //Performing sendKeys operation on the element textBox.sendKeys (`` stqatools '');


11. getAttribute () bid

This dictation find the attribute value of a specified element. It habituate String as the parameter and returns a draw value as its result.

Syntax:

element.getAttribute ();

Example:

WebElement element = driver.findElement (By.id (`` SubmitButton '')); String attValue = element.getAttribute (`` id ''); //This will render `` SubmitButton ''

12. chink () command

The click () command lets the tester replicate the click action on a button, link, radiocommunication push or checkbox. In Webdriver, the click occurs after the ingredient is found. In, the recorder identifies the element, the dictation itself performs the click.

Syntax:

element.click ();

Example:

// Create WebElement WebElement eleclick = driver.findElement (By.id (`` TextBox '')); // Perform click operation eleclick.click (); // OR // Click on any WebElement e.g: Button. driver.findElement (By.id (`` Button_Id '')) .click (); // Create WebElement WebElement eleclick = driver.findElement (By.id (`` TextBox '')); // Perform click operation eleclick.click (); // OR // Click on any WebElement e.g: Button. driver.findElement (By.id (`` Button_Id '')) .click ();

13. getSize () command

This dictation retrieves the stature and width of a specific rendered element. It does not ask a parameter and return the Dimension object as its result.

Syntax:

element.getSize ();

Example:

WebElement element = driver.findElement (By.id (`` SubmitButton '')); Dimension dimension = element.getSize (); System.out.println (“ Height: ” + dimensions.height + ” Width: `` + dimensions.width);

Talk to an Expert

Why use BrowserStack Automate to run Selenium Tests?

You should run Selenium Tests on a real device cloud like for below intellect:

  • Naturalistic Testing Conditions: provide access to a broad spectrum of devices and surroundings, ensuring tests speculate actual exploiter conditions accurately.
  • Enhanced Security: Maintained with high security standards, existent device clouds offer secure, isolated testing environments, minimizing data breach risks.
  • Broad Browser and OS Coverage: Helps name compatibility issues across various browsers and operating systems, enhancing user experience.
  • Performance Insights: Real device yield authentic performance information requirement for optimizing application responsiveness.
  • Scalability and Accessibility: Facilitates scalable and approachable examination, desirable for distributed teams.
  • CI/CD Integration:Integrates smoothly with CI/CD pipelines for continuous testing and early issue detection.
  • Cost-Effectiveness: Although initially more costly, it preserve on long-term disbursement related to fixes and support.

Conclusion

WebElement in Selenium commands are crucial for tester seeking to automatise user activity on a website to verify its performance. The bid remark supra will go a long way in the thorough of website. Selenium tests return the best resultant when run on a.

Tags
100,000+ Views

# Ask-and-Contributeabout this issue with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free