Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

While using Selenium WebDriver to test a website, I would like to have the ability to double click on a WebElement object without having to having to use class inheritance or mess with ActionChains. Ideally, it should be accessible in the webelement.double_click() form, just as click() is. This can be done by editing the WebElement.py file and simply adding the following to the WebElement class:

def double_click(self):
    self._execute(Command.DOUBLE_CLICK)

Simple enough. However, I update this library all the time, and this is liable to get overwritten. With that in mind, I'm trying to figure out a simple way to add this capability to the WebElement object from the file I'm working with. I have tried importing WebElement and defining the function like so:

from selenium import webdriver
from selenium.webdriver.remote.command import Command
from selenium.webdriver.remote.webelement import WebElement

def double_click(self):
    self.execute(Command.DOUBLE_CLICK)

WebElement.double_click = double_click

Then when I run the browser (webdriver.Firefox()), double_click is defined for each element, but it does not function correctly. Instead, it raises

WebDriverException: Message: [JavaScript Error: "Argument to isShown must be of type Element" ...

The same error occurs when I redefine the click() function in the same way. I confirmed that the elements I am attempting to click are class 'selenium.webdriver.remote.webelement.WebElement', but it seems the wires are getting crossed somewhere, and I'm not sure how.

To be clear, I know that there are workarounds for this. The problem is not that I cannot double click - I just want to know if this is possible in a way similar to what I am attempting.

share|improve this question
up vote 0 down vote accepted

To monkey patch the double click method on the WebElement class:

def WebElement_double_click(self):
    self._parent.execute(Command.MOVE_TO, {'element': self._id})
    self._parent.execute(Command.DOUBLE_CLICK)
    return self

WebElement.double_click = WebElement_double_click
share|improve this answer
    
This works. I messed around with it a bit to experiment... As far as I can tell, calling _parent.execute is the same as calling _execute, since the _execute function in webelement.py returns self._parent.execute anyway. It seems the key is the MOVE_TO. What is it that makes the function require this even though it works fine without it if you patch it into the webelement file? – Sam Krygsheld Jul 29 at 13:32
    
The command DOUBLE_CLICK doesn't take any argument (github.com/SeleniumHQ/selenium/wiki/…). So it's better to call execute on the WebDriver since the WebElement will add the id parameter which is not required and MOVE_TO requires the element key and not id. I don't see how it could work without the MOVE_TO command. Maybe if the targeted element is already focused. It could also be that the driver you selected implements the protocol differently. – Florent B. Jul 29 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.