Table Of Contents
- 1 Overview
- 2 List of Selenium WebElement commands
- 2.1 1. sendKeys() command
- 2.2 2. isDisplayed() command
- 2.3 3. isSelected() command
- 2.4 4. submit() command
- 2.5 5. isEnabled() command
- 2.6 6. getLocation() command
- 2.7 7. clear() command
- 2.8 8. getText() command
- 2.9 9. getTagName() command
- 2.10 10. getCssValue() command
- 2.11 11. getAttribute() command
- 2.12 12. click() command
- 2.13 13. getSize() command
- 3 Locators in Selenium WebDriver
- 4 Building better tests with Web Elements in Selenium
- 5 Frequently Asked Questions:
Overview
13 WebElement in Selenium Commands are:
- sendKeys() command
- isDisplayed() command
- isSelected() command
- submit() command
- isEnabled() command
- getLocation() command
- clear() command
- getText() command
- getTagName() command
- getCssValue() command
- getAttribute() command
- click() command
List of Selenium Webelement Commands
Getting good at Selenium means knowing which commands to use and when. Each WebElement in Selenium comes with a toolkit of methods that let you interact with, inspect, and validate elements on a page.
Getting these down means you can handle pretty much any automation scenario that comes your way, so let’s break them down:
1. Sendkeys() Command
The sendKeys() command types text into input fields like search boxes, login forms, or text areas. It simulates actual keyboard input, so you can use it to enter usernames, passwords, or any text your test needs. You can even send special keys like Enter or Tab by using the Keys class.
2. Isdisplayed() Command
This command checks whether an element is visible on the page or not. It returns true if the element is displayed and false if it’s hidden. Super useful for validating if those buttons, messages, or sections actually appear when they’re supposed to.
3. Isselected() Command
The isSelected() command tells you if a checkbox or radio button is currently selected. It returns true if it’s checked and false if it’s not. This one’s essential when you’re verifying form states or testing toggle behavior.
4. Submit() Command
submit() submits a form, just like clicking a submit button would. It works on any form element or element inside a form. Handy when you want to trigger form submission without hunting down the exact submit button.
5. Isenabled() Command
This checks if an element is enabled and can be interacted with. It returns true if the element is active and false if it’s disabled or greyed out. Great for making sure buttons or fields are actually clickable before your script tries to use them.
6. Getlocation() Command
getLocation() gives you the x and y coordinates of an element on the page. This tells you exactly where the element sits relative to the top-left corner of the page. Useful when you need to verify positioning or work with visual layouts.
7. Clear() Command
The clear() command wipes out any existing text in an input field. Before entering new text with sendKeys(), you’ll often want to clear the field first to avoid appending to old data.
8. Gettext() Command
This one retrieves the visible text of an element, like the text inside a button, paragraph, or heading. It’s one of the most common commands you’ll use to validate that the right content appears on your page.
9. Gettagname() Command
getTagName() returns the HTML tag name of an element, like “button”, “input”, “div”, or “a”. Helpful when you need to verify what type of element you’re actually dealing with in your script.
10. Getcssvalue() Command
This command fetches the CSS property value of an element, things like color, font-size, or background-color. Perfect for validating styling or checking if elements match your design specifications.
11. Getattribute() Command
getAttribute() retrieves the value of a specific HTML attribute, like “href” for links, “value” for input fields, or “class” for CSS classes. It’s incredibly versatile for pulling out data that’s stored in element attributes.
12. Click() Command
The click() command does exactly what it sounds like – it clicks an element. Whether it’s a button, link, checkbox, or any clickable item, this is your go-to for simulating user clicks in your automation.
13. Getsize() Command
getSize() returns the height and width of an element in pixels. This is useful when you need to verify element dimensions or ensure responsive design elements are displaying at the right size.
Locators in Selenium Webdriver
Before you can use any of those WebElement commands, you’ve got to actually find the element first, and that’s where locators come in.
Locators are the strategies Selenium uses to hunt down specific elements on a webpage. They are like your search filters that help your script pinpoint exactly which button, text box, or link you want to work with among potentially hundreds of elements on a page.
Selenium WebDriver gives you eight different locator strategies, and each one has its own strengths depending on your situation. Knowing which locator to use when is what separates flaky tests from reliable ones.
ID Locator
The ID locator finds elements using their id attribute. This is typically your first choice because IDs are supposed to be unique on a page: one element, one ID.
When you’ve got a reliable ID, it’s the fastest and most straightforward way to locate web elements in Selenium. Just keep in mind that not every element has an ID, and sometimes they change between releases.
Name Locator
This locator targets elements by their name attribute, which you’ll often see on form fields. It’s particularly common for inputs like username, password, or email fields. Name locators work well for form automation, though unlike IDs, names aren’t guaranteed to be unique, so you might get multiple matches.
CSS Selector Locator
CSS selectors use the same syntax you’d use in stylesheets to target elements. They’re incredibly powerful and flexible; you can combine attributes, navigate parent-child relationships, and use pseudo-classes.
CSS selectors are faster than XPath in most browsers and are a favorite among experienced automation engineers for their balance of power and performance.
Learn the detailed differences between CSS selector and XPath here
Xpath Locator
XPath is the most powerful locator strategy in Selenium. It can traverse the entire DOM tree, move up to parent elements, find elements based on text content, and handle complex conditions that other locators can’t.
The tradeoff is that XPath can be slower and more fragile if you’re not careful about how you write it. Still, when nothing else works, XPath usually can get the job done.
Other Locator Strategies
Beyond the main four, Selenium offers a few more specialized locators that come in handy for specific scenarios:
- Class name: Finds elements by their CSS class attribute. Works when classes are distinctive, but watch out: multiple elements often share the same class.
- Tag name: Locates elements by HTML tag, like input, button, or div. Pretty broad on its own, so best used when finding groups of similar elements.
- Link text: Finds links by their exact visible text. Super readable and great when link text stays consistent.
- Partial link text: Matches links containing a portion of text rather than requiring exact matches. More flexible but riskier if multiple links share similar text.
Building Better Tests with Web Elements in Selenium
You’ve now got the full picture of how WebElement in Selenium actually works – from understanding what these elements are to knowing which commands and locators to use in different situations. But knowing the commands is just the starting point.
A few things to keep in mind: always add waits before interacting with web elements in Selenium. Pages load at different speeds, and elements don’t always appear instantly. Favor stable locators like IDs and CSS selectors over brittle XPath that breaks when the page structure changes.
Don’t overcomplicate things either. The simplest locator that reliably finds your element is usually the best one. If you want to skip the maintenance headaches altogether, Testsigma’s smart locators and built-in waits handle the heavy lifting for you.
Frequently Asked Questions:
WebElements in Selenium are objects that represent HTML elements on a webpage. For instance, buttons, text fields, links, or any interactive component your automation script needs to find and work with.
You can get a list of web elements in Selenium using findElements() method with a locator strategy.
The four types are Selenium IDE (record and playback tool), Selenium WebDriver (browser automation API), Selenium Grid (parallel test execution), and Selenium RC (deprecated legacy version).
WebElement in Selenium is an interface, not a class. It defines the methods for interacting with elements, and browser-specific implementations provide the actual functionality behind those methods.
WebElement (an HTML element) is the interface that allows the Selenium code to interact with various elements on web pages. It allows users to manipulate and assert Web Elements on web pages by selecting, clicking, entering data, etc.
No, size() is not a WebElement method. It is a method of the built-in Java class java. util.Collection.