Skip to content
CodeKruPro

Double-Click a Button

57/80

Double-Click a Button

double_click sends two clicks. Read the text before and after.

Task:

  1. Print the text of #doubleClick
  2. Double-click it
  3. Print the text again

Print the lines below exactly.

Expected output:

Before: Double click on me
After: I was double-clicked!
Read full tutorial on Codekru

About this lesson

Selenium Python double_click with ActionChains: trigger a double-click handler

Selenium Python AssignmentsAdvancedMouse and Keyboard

How it works

A double click is two quick clicks plus a separate dblclick event. Two separate click() calls do not fire that event. The ActionChains method double_click sends the right sequence. You pass the target element, then call .perform() to run the stored steps.

You can see the difference on this page. A normal click() on the button left its text unchanged. Only the double click switched it. When a feature listens for the double click event, a single click is the wrong tool.

Every chain method returns the chain, so more steps can follow before perform(). The final call returns None, so do not expect a result from it. The gesture belongs to the chain only. Asking the element itself raises AttributeError: 'WebElement' object has no attribute 'double_click'.

You can keep the variable you looked up before the gesture. The button stays on the page, its text changes and it becomes disabled, and reading .text again still works. A page that reloads is different, and old elements go stale there.

About the assignment

Show that a double click changes a button. Read its label, perform the gesture, and read the label again.

What you will do

  1. Locate the double-click button by its id and print its label under the first heading from the task.
  2. Build a chain that double clicks that element and run it.
  3. Read the same element's .text again and print it under the second heading.

Common mistakes

  • Using a single click

    On this page click() leaves the text as it was. The handler waits for the double click.

  • Building the chain and stopping

    Without perform() the object is only a plan. The text afterwards is unchanged, and Python raises nothing.

  • Calling the gesture on the element

    The line fails with AttributeError, since the method belongs to ActionChains.

In a real test suite

Real apps use double click for rename-in-place and spreadsheet-like cells. Suites hide the chain in a helper that takes an element, so every test calls one clear function.

The next lesson swaps the gesture for a right click on a neighbouring button.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Buttons")
    # Double-click #doubleClick and print both texts
finally:
    driver.quit()
Read the full tutorial