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 the commonly used element operations in Selenium and their detailed descriptions.
| Operation Type | Method | Description | Example |
|---|---|---|---|
| Input Text | send_keys("text") | Enter the specified text into an input box or text area. | element.send_keys("testuser") |
| Click 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 Attribute | get_attribute("attribute_name") | Get the specified attribute value of an element (e.g.,value、href、classetc.). | value = element.get_attribute("value") |
| Get Element Text | text | Get the visible text content of an element. | print(element.text) |
| Checkbox/Radio Button Operations | is_selected()andclick() | Check whether a checkbox or radio button is selected, and select or deselect it. | if not checkbox.is_selected(): checkbox.click() |
| Dropdown List Operation | Selectof the Select classselect_by_index、select_by_value、select_by_visible_text | Select an option from the dropdown list by index, value, or visible text. | dropdown.select_by_visible_text("China") |
| Mouse Operations | ActionChainsof the ActionChains classclick、double_click、context_click、drag_and_drop、move_to_element | Perform complex mouse operations such as clicking, double-clicking, dragging and dropping, and hovering. | actions.move_to_element(element).perform() |
| Keyboard Operations | Keysof the Keys classsend_keys(Keys.KEY_NAME) | Simulate keyboard operations, such as pressing Enter, Tab, or key combinations. | element.send_keys(Keys.ENTER) |
| File Upload | send_keys("file_path") | Use the<input type="file">element to upload a file. | element.send_keys("/path/to/file.txt") |
| Execute JavaScript | execute_script("javascript_code") | Execute JavaScript code for complex operations or modifying page content. | driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") |
1. Input Text (send_keys)
In automated testing, it is often necessary to enter text into an input box. Selenium provides thesend_keysmethod to achieve this functionality.
Example
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 the webpage
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 locate an input box element, and then use thesend_keysmethod to enter the text "your_username" into it.
2. Click Element (click)
Clicking is one of the most common operations in automated testing. Selenium provides theclickmethod to simulate a user clicking an element.
Example
button_element = driver.find_element_by_id("submit-button")
# Click the button
button_element.click()
In this example, we locate a button element and use theclickmethod to simulate the user clicking the button.
3. Clear Input Box Content (clear)
Sometimes we need to clear the existing content in an input box before entering new content. Selenium provides theclearmethod to achieve this functionality.
Example
input_element = driver.find_element_by_name("username")
# Clear the input box content
input_element.clear()
# Enter new text
input_element.send_keys("new_username")
In this example, we first clear the content in the input box, and then enter the new text "new_username".
4. Get Element Attribute (get_attribute)
In automated testing, it is sometimes necessary to get the attribute value of an element. Selenium provides theget_attributemethod to get the attributes of an element.
Example
element = driver.find_element_by_id("element-id")
# Get the element's attribute value
attribute_value = element.get_attribute("class")
print(attribute_value)
In this example, we get an element'sclassattribute value and print it out.
5. Get Element Text (text)
Sometimes we need to get the text content inside an element. Selenium provides thetextproperty to get the text of an element.
Example
element = driver.find_element_by_id("element-id")
# Get the element's text content
text_content = element.text
print(text_content)
In this example, we get an element's text content and print it out.
6. Checkbox and Radio Button Operations
Checkboxes and radio buttons are common elements in web forms. Selenium provides theclickmethod to operate these elements.
Checkbox Operation
Example
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 it is not selected, we click to select it.
Radio Button Operation
Example
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()
The operation of radio buttons is similar to checkboxes. We first check whether the radio button is already selected. If it is not selected, we click to select it.
7. Dropdown List Operation (Select Class)
Dropdown lists are one of the common elements in web forms. Selenium provides theSelectclass to conveniently operate dropdown lists.
Example
# 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 use theSelectclass to operate it. We can select options in the dropdown list by visible text, value, or index.
Summary
This article has introduced in detail the common element operations in Selenium, including entering text, clicking elements, clearing input box content, getting element attributes and text, operating checkboxes and radio buttons, and handling dropdown lists. Mastering these basic operations is key to automated testing. I hope this article helps you better understand and use Selenium for web automated testing.
Other Extensions