Selenium + Python · Lesson 17 of 43

0/43 done

Module 17: WebElement

17. WebElement Operations: Typing, Clicking & State Checks

16 minlesson

Your task

Implement safe_type(element, text_to_enter):

  1. If element is_displayed() and is_enabled():
    • element.clear()
    • element.send_keys(text_to_enter)
    • return True
  2. Return False otherwise.

Write it in the editor, then press Check answer (Ctrl + Enter).

Interacting with WebElements

python
username_field = driver.find_element(By.ID, "username")

# Input operations:
username_field.clear()
username_field.send_keys("sdet_python")

# Reading attributes & states:
val = username_field.get_attribute("value")
is_visible = username_field.is_displayed()
is_active = username_field.is_enabled()

Key Takeaways & Walkthrough Summary

WebElement objects represent the HTML nodes you interact with on the page. In this lesson, you will learn to send keys, clear inputs, read element text and attributes, and check visibility before interacting. Let's master WebElement!

Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why does element.text return an empty string for an <input> element?

Text typed into inputs lives in the 'value' HTML attribute. Use element.get_attribute('value') to retrieve input text.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip