Selenium Element Operations
In Selenium, after locating an element, you can perform various operations on it, such as clicking, entering text, and getting attributes.
The following are common element operations in Selenium and their detailed descriptions.
| Operation types | Method | Description | Example |
|---|---|---|---|
| Enter text | send_keys("text") |
Enter the specified text into an input box or text area. | element.send_keys("testuser") |
| Click an element | click() |
Click a button, link, or other clickable element. | element.click() |
| Clear input box content | clear() |
Clear the content in an input box or text area. | element.clear() |
| Get element attributes | get_attribute("attribute_name") |
Get the element's specified attribute value (such as value, href, class etc.). |
value = element.get_attribute("value") |
| Get element text | text |
Get the element's visible text content. | print(element.text) |
| Checkbox/Radio button operations | is_selected() and click() |
Check whether a checkbox or radio button is selected, and select or deselect it. | if not checkbox.is_selected(): checkbox.click() |
| Dropdown list operations | Select The class's select_by_index, select_by_value, select_by_visible_text |
Select an option from a dropdown list by index, value, or visible text. | dropdown.select_by_visible_text("China") |
| Mouse operations | ActionChains The class's click, double_click, context_click, drag_and_drop, move_to_element |
Perform complex mouse operations such as click, double-click, drag-and-drop, hover, etc. | actions.move_to_element(element).perform() |
| Keyboard operations | Keys The class's send_keys(Keys.KEY_NAME) |
Simulate keyboard operations, such as pressing Enter, Tab, and key combinations. | element.send_keys(Keys.ENTER) |
| File upload | send_keys("file_path") |
Pass Upload a file via an element. |
element.send_keys("/path/to/file.txt") |
| Execute JavaScript | execute_script("javascript_code") |
Execute JavaScript code for complex operations or to modify page content. | driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") |
1. Entering Text (send_keys)
In automated testing, it is often necessary to enter text into input boxes; Selenium providessend_keysmethod.
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Launch the browser
driver
=
webdriver.
Chrome
(
)
Open a web page
driver.
get
(
"https://example.com"
)
Locate the input box element
input_element
=
driver.
find_element_by_name
(
"username"
)
Enter text
input_element.
send_keys
(
"your_username"
)
Close the browser
driver.
quit
(
)
In the code above, we first located an input box element, then usedsend_keysthe method to enter the text "your_username" into it.
2. Clicking an Element (click)
Clicking is one of the most common operations in automated testing. Selenium providesclicka method to simulate a user clicking an element.
Example
Locate the button element
button_element
=
driver.
find_element_by_id
(
"submit-button"
)
Click the button
button_element.
click
(
)
In this example, we located a button element and usedclickthe method to simulate the user clicking that button.
3. Clearing Input Box Content (clear)
Sometimes we need to clear existing content in an input box before entering new content. Selenium providesclearmethod.
Example
Locate the input box element
input_element
=
driver.
find_element_by_name
(
"username"
)
Clear input box content
input_element.
clear
(
)
Enter new text
input_element.
send_keys
(
"new_username"
)
In this example, we first cleared the content of the input box, then entered the new text "new_username".
4. Getting Element Attributes (get_attribute)
In automated testing, it is sometimes necessary to get an element's attribute value. Selenium providesget_attributea method to get the element's attributes.
Example
Locate the element
element
=
driver.
find_element_by_id
(
"element-id"
)
Get an element's attribute value
attribute_value
=
element.
get_attribute
(
"class"
)
(
attribute_value
)
In this example, we got an element'sclassattribute value and printed it out.
5. Getting Element Text (text)
Sometimes we need to get the text content inside an element. Selenium providestextan attribute to get the element's text.
Example
Locate the element
element
=
driver.
find_element_by_id
(
"element-id"
)
Get the element's text content
text_content
=
element.
text
(
text_content
)
In this example, we got an element's text content and printed it out.
6. Checkbox and Radio Button Operations
Checkboxes and radio buttons are common elements in web forms. Selenium providesclicka method to operate these elements.
Checkbox operations
Example
Locate the checkbox element
checkbox_element
=
driver.
find_element_by_id
(
"checkbox-id"
)
If the checkbox is not selected, click to select it
if
not
checkbox_element.
is_selected
(
)
:
checkbox_element.
click
(
)
In this example, we first check whether the checkbox is already selected; if not, we click to select it.
Radio button operations
Example
Locate the radio button element
radio_button_element
=
driver.
find_element_by_id
(
"radio-button-id"
)
If the radio button is not selected, click to select it
if
not
radio_button_element.
is_selected
(
)
:
radio_button_element.
click
(
)
Radio button operations are similar to checkboxes; we also first check whether the radio button is already selected, and if not, click to select it.
7. Dropdown List Operations (Select class)
Dropdown lists are one of the common elements in web forms. Selenium providesSelecta class to conveniently operate dropdown lists.
Example
from
selenium.
webdriver
.
support
.
ui
import
Select
Locate the dropdown list element
dropdown_element
=
driver.
find_element_by_id
(
"dropdown-id"
)
Create a Select object
select
=
Select
(
dropdown_element
)
Select an option by visible text
select
.
select_by_visible_text
(
"Option 1"
)
Select an option by value
select
.
select_by_value
(
"1"
)
Select an option by index
select
.
select_by_index
(
0
)
In this example, we first locate a dropdown list element, and then useSelecta class to operate on it. We can select options in the dropdown list by visible text, value, or index.
Summary
This article details common element operations in Selenium, including entering text, clicking elements, clearing input fields, retrieving element attributes and text, handling checkboxes and radio buttons, and working with dropdown lists. Mastering these basic operations is key to automated testing. We hope this article helps you better understand and use Selenium for web automation testing.