Element Methods in Selenium Python

Selenium is an open-source automation testing tool used with programming languages like Python, Java, JavaScript, and Perl to test web applications. It provides various element methods to interact with web page components programmatically.

Essential Element Methods

1. send_keys()

Used for entering text into input fields, text areas, and form elements. Also supports modifier keys like Ctrl, Alt, and Shift.

Return Type: None

2. is_selected()

Checks if an element like checkbox, radio button, or option is selected.

Return Type: Boolean (True or False)

3. is_displayed()

Verifies if an element is visible on the web page.

Return Type: Boolean (True or False)

4. get_property()

Retrieves the value of a specified property of an element.

Return Type: String (property value)

5. is_enabled()

Determines if an element is enabled and can be interacted with.

Return Type: Boolean (True or False)

6. click()

Simulates a mouse click on an element like buttons, links, or clickable components.

Return Type: None (navigates to linked page or throws exception if invalid)

7. text

Retrieves the visible text content of an element.

Return Type: String (text content)

8. location

Gets the position coordinates of an element on the page.

Return Type: Dictionary with 'x' and 'y' coordinates

9. screenshot()

Captures a screenshot of the current element and saves it to a specified path.

Return Type: Boolean (True if successful, False on error)

Basic Web Navigation Example

Here's how to set up Selenium WebDriver and navigate to a website ?

# Import webdriver
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# Create webdriver object - Windows/Linux users
driver = webdriver.Chrome()

# For Mac users (Enable Remote Automation in Safari Develop menu)
# driver = webdriver.Safari()

# Navigate to website
driver.get("https://www.tutorialspoint.com/")

# Close the browser
driver.quit()

Element Interaction Example

This example demonstrates finding elements and using various element methods ?

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Initialize webdriver
driver = webdriver.Chrome()
driver.get("https://www.tutorialspoint.com/")

# Find an element and check its properties
try:
    # Find element by class name
    element = driver.find_element(By.CLASS_NAME, "header--navbar")
    
    # Check element properties
    print("Is displayed:", element.is_displayed())
    print("Is enabled:", element.is_enabled())
    print("Element location:", element.location)
    print("Element text:", element.text)
    
    # Take screenshot of the element
    element.screenshot('/path/to/save/header_screenshot.png')
    
    time.sleep(2)
    
except Exception as e:
    print(f"Error: {e}")
    
finally:
    driver.quit()

Method Comparison

Method Purpose Return Type Common Use Case
send_keys() Input text None Form filling
click() Click element None Button/link interaction
is_displayed() Check visibility Boolean Element validation
text Get text content String Data extraction

Best Practices

Always use explicit waits: Wait for elements to be present before interacting with them.

Handle exceptions: Use try-except blocks to handle element not found errors gracefully.

Close browser sessions: Always call driver.quit() to properly close the browser and free resources.

Conclusion

Selenium element methods provide powerful tools for web automation testing. These methods enable efficient interaction with web elements, validation of page states, and automated testing workflows. Proper use of these methods forms the foundation of robust web automation scripts.

Updated on: 2026-03-27T13:17:24+05:30

827 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements