Exploring Selenium Web Element Methods in Python

Exploring Selenium WebElement Methods in Python


Introduction

Selenium is a popular tool for automating web browsers using code.
In Selenium, every element on a web page — like buttons, links, and text boxes — is treated as a WebElement.

To interact with these elements, Selenium provides many helpful WebElement methods.

This blog will help you explore and understand the most important Selenium WebElement methods using Python.


What Is a WebElement?

A WebElement is an object that represents a single element on a webpage.
Before you can use any method, you first need to find the element using a locator:

from selenium import webdriver driver = webdriver.Chrome() driver.get("https://example.com") element = driver.find_element("id", "username")

Now you can use methods like .click(), .send_keys(), .text, and more on the element.


1. click()

Used to click on a button, link, checkbox, etc.

login_button = driver.find_element("id", "login") login_button.click()

2. send_keys()

Used to type text into input fields.

username = driver.find_element("id", "username") username.send_keys("my_user_name")

3. clear()

Used to clear the input field before typing new text.

username.clear() username.send_keys("new_user")

4. text

Returns the visible text of an element.

message = driver.find_element("id", "welcome").text print("Message:", message)

5. get_attribute("attribute_name")

Gets the value of an attribute like href, value, or class.

link = driver.find_element("tag name", "a") href = link.get_attribute("href") print("Link goes to:", href)

6. is_displayed()

Checks if the element is visible on the page.

element = driver.find_element("id", "popup") if element.is_displayed(): print("Popup is visible")

7. is_enabled()

Checks if the element is enabled (can be clicked or typed into).

submit_button = driver.find_element("id", "submit") if submit_button.is_enabled(): submit_button.click()

8. is_selected()

Used mainly for checkboxes and radio buttons.

checkbox = driver.find_element("id", "agree") if not checkbox.is_selected(): checkbox.click()

9. submit()

Submits a form element.

form = driver.find_element("id", "loginForm") form.submit()

10. location

Returns the x and y coordinates of the element on the screen.

button = driver.find_element("id", "submit") print(button.location)

11. size

Returns the height and width of the element.

logo = driver.find_element("id", "logo") print(logo.size)

12. tag_name

Gives the tag name of the element (e.g., div, input, button).

element = driver.find_element("id", "email") print(element.tag_name)

13. value_of_css_property("property")

Returns the value of a CSS property, like color, font-size, etc.

button = driver.find_element("id", "submit") color = button.value_of_css_property("color") print("Button color:", color)

14. screenshot(filename)

Takes a screenshot of the element and saves it to a file.

element = driver.find_element("id", "captcha") element.screenshot("captcha.png")

Example: Login Form Automation

from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://example.com/login") username = driver.find_element(By.ID, "username") password = driver.find_element(By.ID, "password") login_btn = driver.find_element(By.ID, "loginBtn") username.send_keys("admin") password.send_keys("1234") login_btn.click() print("Logged in:", driver.find_element(By.ID, "welcome").text)

WebElement Best Practices

✅ Always wait for elements using WebDriverWait
✅ Use clear locators (id, name, CSS, XPath)
✅ Use .clear() before .send_keys()
✅ Check .is_displayed() before interacting
✅ Take screenshots for debugging


Debugging Tip: Element Not Found

If you get:

NoSuchElementException

✅ Check:

  • Correct locator (id/class/XPath)

  • If the element is inside an iframe

  • If the page has fully loaded

Use WebDriverWait like:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "username")) )

Advanced Methods (Bonus)

1. find_elements()

Returns a list of WebElements

links = driver.find_elements(By.TAG_NAME, "a") print("Number of links:", len(links))

2. shadow_root

Used to access elements inside Shadow DOM (in newer web apps)

shadow_host = driver.find_element(By.CSS_SELECTOR, "#host") shadow_root = shadow_host.shadow_root element = shadow_root.find_element(By.CSS_SELECTOR, "button") element.click()

Conclusion

Selenium WebElement methods are the backbone of browser automation in Python.
By learning and using these methods, you can:

  • Fill forms

  • Click buttons

  • Extract data

  • Test complete user flows

  • Automate websites with confidence

Whether you're a beginner or advanced tester, mastering these WebElement methods will help you build powerful and reliable test scripts.



Read More 




Comments

Popular posts from this blog

What is WebDriver in Selenium? Explained with Java

Why Choose Python for Full-Stack Web Development

How Generative AI Differs from Traditional AI