Skip to content
CodeKruPro

Automation tool (Selenium) · Python

Clicks, typing and reading elements

Clicking, typing, reading text and attributes, and element state.

Learn: Clicking, typing and reading elements in Selenium Python7 minute read

After finding an element you use it: click it, type into it, read it and check its state. Every test is built from these steps. Each one has a small trap that interviewers test, and Python adds one more: some reads are properties, written without brackets, and some are methods.

Click and type

name = driver.find_element(By.ID, "firstName")
name.clear()
name.send_keys("Asha")
driver.find_element(By.ID, "save").click()

send_keys adds to the existing value, so clear a pre-filled field first. click() behaves like a user: it scrolls the element into view and clicks its centre. A hidden element raises ElementNotInteractableException; a covered one raises ElementClickInterceptedException. Treat both as a sign to wait or fix the page state.

Special keys come from selenium.webdriver.common.keys.Keys: name.send_keys(Keys.ENTER), or name.send_keys(Keys.CONTROL, "a") to select all (Keys.COMMAND on a Mac).

Properties and methods

element.text, element.tag_name, element.size, element.location, element.rect and element.shadow_root are properties, read without brackets. get_attribute(), is_displayed() and the other checks are methods. Writing element.text() raises TypeError, a common slip under interview pressure.

Reading text versus attributes

An attribute is a name and value written in the HTML tag, such as value="init@x.com". A property is the live value the browser keeps, which changes as the user types.

  • element.text is the visible text, with spaces tidied. A hidden element gives an empty string, and so does an input field.
  • get_attribute("value") returns the current value, including what the test typed. It returns the live property when there is one, otherwise the HTML attribute.
  • get_property("value") reads the live property only.
  • get_dom_attribute("value") reads the attribute as written in the HTML, so after typing it still holds the original value.

A small helper keeps tests short and is the first step towards page objects (classes that keep the locators and actions of one page together):

def text_of(driver, locator):
    return driver.find_element(*locator).text.strip()

CSS values and colours

value_of_css_property("color") returns the computed style, the final value after all style rules apply. Chrome reports red text as rgba(255, 0, 0, 1): the red, green and blue amounts (0 to 255) and the opacity (1 means fully solid). Compare colours through Selenium's helper:

from selenium.webdriver.support.color import Color

colour = driver.find_element(By.ID, "price").value_of_css_property("color")
assert Color.from_string(colour).hex == "#ff0000"

An assert statement fails the test when the condition is false.

State checks

  • is_displayed(): visible on the page.
  • is_enabled(): usable, not disabled.
  • is_selected(): a checkbox ticked, a radio button chosen, an option picked.

They are independent of each other. A click toggles a checkbox, so check first:

terms = driver.find_element(By.ID, "terms")
if not terms.is_selected():
    terms.click()
assert terms.is_selected(), "terms checkbox should be ticked"

Radio buttons in one group clear each other. To prove a control is disabled, assert not is_enabled().

Tooltips and date pickers

A tooltip is the small box of text that appears when the mouse rests on something. A native tooltip is the element's title attribute; the box the browser draws is outside the page, so read get_attribute("title"). A custom tooltip appears as a page element on hover: hover (see Mouse, keyboard and JavaScript), wait for it, read it. For a date picker, type the date when the input allows it; otherwise step through the calendar in a bounded loop and click the day. Compute dates in code, for example datetime.date.today() + datetime.timedelta(days=7), instead of hard-coding them.

How to answer in an interview

Start with the difference that causes bugs: .text is what a user sees, the value is what a field holds. Show the matching call, then name the three state checks with one example each. Finish every read with an assert and a message; with pytest a failed assert shows both sides, so the report explains itself.

Common mistakes

  • Reading an input with .text and getting an empty string.
  • Calling .text() as if it were a method.
  • Typing into a pre-filled field without clear().
  • Clicking a checkbox without checking its state first.
  • Printing values instead of asserting them.

Recap

Clear before typing and let click() fail when the page is not ready. Read visible text with .text and field values with get_attribute or get_property. Check state with is_displayed, is_enabled and is_selected, and assert every value you read.

21 questions0/21 solved

StatusQuestionDifficultyAsked in
Not startedtext, get_attribute, get_dom_attribute and get_propertyMediumAsked in: Accenture, Capgemini
Not startedDisplayed, enabled and selected: which check fits?EasyAsked in: Accenture, Capgemini
Not startedClick a button and read its new textEasy
Not startedType into a text field and read the value backEasyAsked in: Accenture, Capgemini
Not startedClear a pre-filled field and type a new valueEasy
Not startedRead the visible text of elementsEasy
Not startedRead attribute values of an inputEasyAsked in: Accenture, Capgemini
Not startedRead a CSS property of an elementEasy-MediumAsked in: Accenture, Infosys
Not startedSelect a checkbox and verify its stateEasyAsked in: Accenture, Capgemini
Not startedClear a checkbox that starts selectedEasyAsked in: Accenture, Capgemini
Not startedChoose one radio button in a groupEasyAsked in: Accenture, Capgemini
Not startedTell a disabled control from an enabled oneEasyAsked in: Accenture, Capgemini
Not startedCheck whether an element is displayedEasyAsked in: Accenture, Capgemini
Not startedCheck whether a text field is enabledEasyAsked in: Accenture, Capgemini
Not startedCheck whether a checkbox is selectedEasyAsked in: Accenture, Capgemini
Not startedAssert the text you just readEasy
Not startedWrite a function that returns element textEasy
Pro+Reading the text of a hidden elementEasy-Medium
Pro+Verify an element's colour (Python)MediumAsked in: Accenture, Infosys
Pro+How do you verify a tooltip's text?Easy-MediumAsked in: Accenture, TCS
Pro+Select a date in a date pickerMedium
Not startedNot startedIn progressIn progressSolvedSolvedPro+Pro+

The first 2 questions of each topic are free. Pro+ opens every question.

Back to the plan