YouTip LogoYouTip

Selenium Ele Operator

Selenium Element Operations | Beginner's Tutorial

In Selenium, after locating an element, you can perform various operations on it, such as clicking, inputting text, getting attributes, etc.

Below are the commonly used element operations in Selenium and their detailed explanations.

Operation Type Method Description Example
Input Text send_keys("text") Input 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 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 (such as value, href, class, etc.). value = element.get_attribute("value")
Get Element Text text Get the visible text content of an element. print(element.text)
Checkbox/Radio Button Operation 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 Operation Select class's select_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 Operation ActionChains class's click, double_click, context_click, drag_and_drop, move_to_element Perform complex mouse operations, such as clicking, double-clicking, dragging and dropping, hovering, etc. actions.move_to_element(element).perform()
Keyboard Operation Keys class's send_keys(Keys.KEY_NAME) Simulate keyboard operations, such as pressing Enter, Tab, combination keys, etc. element.send_keys(Keys.ENTER)
File Upload send_keys("file_path") Upload a file through the <input type="file"> element. 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 input text into input boxes. Selenium provides the send_keys method to achieve this functionality.

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 browser
driver = webdriver.Chrome()

# Open webpage
driver.get("https://example.com")

# Locate input element
input_element = driver.find_element_by_name("username")

# Input text
input_element.send_keys("your_username")

# Close browser
driver.quit()

In the code above, we first located an input element, then used the send_keys method to input the text "your_username" into it.

2. Click Element (click)

Clicking is one of the most common operations in automated testing. Selenium provides the click method to simulate user clicks on elements.

Example

# Locate button element
button_element = driver.find_element_by_id("submit-button")

# Click button
button_element.click()

In this example, we located a button element and used the click method to simulate the user clicking on that button.

3. Clear Input Content (clear)

Sometimes we need to clear existing content in an input box before inputting new content. Selenium provides the clear method to achieve this functionality.

Example

# Locate input element
input_element = driver.find_element_by_name("username")

# Clear input content
input_element.clear()

# Input new text
input_element.send_keys("new_username")

In this example, we first cleared the content in the input box, then input the new text "new_username".

4. Get Element Attribute (get_attribute)

In automated testing, it is sometimes necessary to get attribute values of elements. Selenium provides the get_attribute method to get element attributes.

Example

# Locate element
element = driver.find_element_by_id("element-id")

# Get element attribute value
attribute_value = element.get_attribute("class")

print(attribute_value)

In this example, we got the class attribute value of an element and printed it out.

5. Get Element Text (text)

Sometimes we need to get the text content inside an element. Selenium provides the text attribute to get element text.

Example

# Locate element
element = driver.find_element_by_id("element-id")

# Get element text content
text_content = element.text

print(text_content)

In this example, we got the text content of an element and printed it out.

6. Checkbox and Radio Button Operations

Checkboxes and radio buttons are common elements in web forms. Selenium provides the click method to operate these elements.

Checkbox Operation

Example

# Locate checkbox element
checkbox_element = driver.find_element_by_id("checkbox-id")

# If 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, and if not, click to select it.

Radio Button Operation

Example

# Locate radio button element
radio_button_element = driver.find_element_by_id("radio-button-id")

# If 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 checkbox operations. We also first check whether the radio button is already selected, and if not, click to select it.

7. Dropdown List Operation (Select Class)

Dropdown lists are one of the common elements in web forms. Selenium provides the Select class to conveniently operate dropdown lists.

Example

from selenium.webdriver.support.ui import Select

# Locate dropdown list element
dropdown_element = driver.find_element_by_id("dropdown-id")

# Create Select object
select = Select(dropdown_element)

# Select option by visible text
select.select_by_visible_text("Option 1")

# Select option by value
select.select_by_value("1")

# Select option by index
select.select_by_index(0)

In this example, we first located a dropdown list element, then used the Select class to operate it. We can select options in the dropdown list by visible text, value, or index.

Summary

This article detailed the common element operations in Selenium, including inputting text, clicking elements, clearing input content, getting element attributes and text, operating checkboxes and radio buttons, and handling dropdown lists. Mastering these basic operations is key to performing automated testing. We hope this article helps you better understand and use Selenium for web automated testing.

← Selenium Browser OperatorSelenium Webdriver β†’