WebElement
Interface:
WebElement
is a predefined interface in Selenium.
The WebElement
interface in Selenium represents any HTML element on a web page.
Examples
include text boxes, radio buttons, checkboxes, and visible text.
Each
element on a web page is considered a web element (or) whatever we see in the web page is a web
element
Any
item visible on the web page is a web element.
Examples:
first name text box, last name text box, male radio button, female radio
button, electronic checkbox
Visible text à is also web element
Every element has some properties/
attributes with specific "values"
Inspecting Elements:
To interact with web elements, you first need to inspect them to understand
their attributes.
Steps:
· Right-click
on the desired/target element.
· Select
"Inspect" from the context menu.
·
The browser's Developer Tools will open, displaying the HTML code with
the selected element highlighted.
Homework:
· Inspect Last Name Text Box:
· Inspect Male Radio Button:
·
Inspect Female Radio Button:
Moving Developer Tools (or) docker:
· Move to
Right: Click the three-dot menu on the Developer Tools panel, select
"Dock side," and choose the right option.
· Move to
Bottom: Click the three-dot menu, select "Dock side," and choose
the bottom option.
·
Move to Left Side: Click the three-dot menu, select "Dock
side," and choose the left option.
Highlighting
Elements:
- Hover over different tags in
the HTML code. The corresponding elements on the web page will be
highlighted.
Actions on Web Elements:
For each web element, specific actions can be performed using methods
provided by the WebElement
interface.
ex: send data to textbox ele, ---- >
specific Method
clear data from textbox ele --> specific Method
get val from text box ele --> specific Method
ele is displayed -->
ele
is enabled à
ele
is selected à
Radio btn : click
---> click()
Checkbox
: clicking/ unclicking --> click()
Select/ Unselect checkbox
Important Methods in the WebElement Interface:
We have to know what method we have to use.
- void sendKeys(String val):
Sends or enters data into a text box.
sendkeys("Ramu");
Examples:
- User name text box
- Password text box
2. void click():
Clicks an element like a button, radio button, or checkbox.
Examples:
·
Click
login button
·
Select a
radio button
·
Select a
checkbox
3. void clear():
Clears the value from a text box.
4.
boolean isDisplayed():
It can be used to check the element is displayed or
not in webpage.
- Returns true if the element is
displayed.
- Throws NoSuchElementException if the element is not found
but not false
5. boolean isEnabled():
Checks if the element is enabled.
- Returns true if the element is enabled.
- Throws an exception if the
element is not found.
6. boolean isSelected():
It
can be used for checkbox , radio button
whether it is selected
- Returns true if the checkbox/radio
button is selected.
- Returns false if the checkbox/radio
button is not selected.
- Returns false if the checkbox/radio
button is not selected.
7. String getAttribute(String attrName):
Gets the value of the specified attribute of the element.
Example
element:
html
<input type="text" value="default
value Ram" name="firstname" id="idfirst" class="firstclass"
width="55">
Usage:
- getAttribute("type") returns "text"
- getAttribute("name") returns "firstname"
- getAttribute("id") returns "idfirst"
Who
defines these properties (name, id) for HTML tags or elements in HTML?
- The developer defines
these properties, not the testers
8. String getText():
Gets the visible text of the specified element. This method is generally used
to get the text content of elements like <a>, <option>, <div>, <span>, <ul>, and <li>.
<a > My gmail
</a>
<option> ECE
</option>
<div> Ramesh </div>
<span> Tendulkar </span>
<ul> Mobile </ul>
<li>Samsung </li>
Note:
Don't use this method for text box elements.
If you use it on a text box element, it will not return the value inside the
text box. Instead, it will return an empty string or potentially throw an
exception, depending on the state of the text box.
9. void
submit():
Submits a form. This method can be used on elements within a form, like an
input of type "submit" or "button".
<input type="submit"
value="Submit">
<button
type="submit">Login</button>
<input type="button"
value="Click me">
Note: You can also use the click() method for buttons.
10. findElement(By by):
The findElement(By by)
method in Selenium is used to find/locate/identify an element on a web page
using the By class locators (or) By class mechanism. It returns the first matched
element only
By is a predefined class in
Selenium that provides methods like name(),
id(), className(), xpath(), etc., to locate
elements.
Refer to the
Selenium API documentation for more details.
By Class Locators:
The By class in Selenium
is used to locate elements. It contains both abstract and non-abstract methods.
- Abstract Methods: These
methods are declared without implementation. The implementation is provided
by subclasses.
- Non-Abstract Methods:
These methods are fully implemented.
By.id("idValue");
By.name("nameValue");
By.className("classValue");
By.xpath("xpathExpression");
By.cssSelector("cssSelector");
By.linkText("fullLinkText");
By.partialLinkText("partialLinkText");
By.tagName("tagName");
Revision:
Webdriver
: Interface or class -->
-> Interface
def: ideally represents any type of browser.
=
browser
Webelement
: Interface or class name ? -> Interface
def:
ChromeDriver
--> Interface or Class ---> Class
A class that
implements WebDriver to control the Chrome browser.
Inspecting/View Properties of Elements:
- Steps: Right-click on the
desired element and select "Inspect" to view its properties.
Questions on WebDriver Methods:
open
url --> get("url") or
navigate()
to url --> navigate().to("url")
get
title of page - > getTitle() return type string
get
current url --> getCurrentUrl() String
close
browser --> close();
close
all browsers --> quit()
Questions on WebElement Methods:
sendkeys("ram")
; -->
- textbox
click()
; -->
click the given ele
clear()
; --> clear the val from text box
isDisplayed()
--> returns ?
isenabled
--> returns ?
isselected
--> returns ?
getattribute("name")
; //
get val
The getAttribute() method returns
null if the specified
attribute does not exist.
getText()
;
No comments:
Post a Comment