in my testing I need to click on a 'link' or a 'button' (actually it just looks like and functions as a link or a button) that is implemented as a div (or span) element. With Selenium IDE, I tried with either clickAt or mouseDown command, both worked fine to successfully click that 'link' or 'button'. However, in WebDriver, it seems that currently there is no click_at method (using Python binding), can anyone please help me on this, or is there any workaround available? Thanks.
P.S. As an example, I noticed that in Gmail, the "COMPOSE" button is implemented as a div element.
-
Did you create a custom locator to find the span element? I've found with the Java Bindings that you can't locate a link such as this with the linkText FindBy annotation.Scott– Scott2012年03月28日 16:39:55 +00:00Commented Mar 28, 2012 at 16:39
-
Personally I would raise a div that is implemented as a button as a defect. HTML is supposed to be sematically correct, by subverting the functionality of elements this is no longer true and it will affect people interacting with your site. How will a screen reader know a div is really a button? How will people who have customised css for accessibility purposes know that the div's are supposed to be buttons? How will somebody with JS turned off interact with the site?Ardesco– Ardesco2012年03月29日 09:52:44 +00:00Commented Mar 29, 2012 at 9:52
-
I know that things like this can be done, but I would argue that they shouldn't be done and by using workarounds in our automation to get round this badly written HTML we are just adding to the problem. Say no to bad HTML!Ardesco– Ardesco2012年03月29日 09:52:58 +00:00Commented Mar 29, 2012 at 9:52
3 Answers 3
Apologies - I don't know Python, but in C# I'm able to click elements with any tag by using MoveToElement()
before clicking. This way Webdriver is clicking based on location. For example:
new Actions(driver).MoveToElement(mySpanElement).Click().Perform();
Something similar might work for Python.
-
Great thanks @SteveCZ, it is working in Python with similar method as: <br/>ActionChains(driver).move_to_element(targetElement).click().perform()Andy– Andy2012年03月27日 16:20:25 +00:00Commented Mar 27, 2012 at 16:20
-
Java also has very similar API, helped me to figure that out, thanks!ImtiazeA– ImtiazeA2021年03月02日 05:07:05 +00:00Commented Mar 2, 2021 at 5:07
private void mouseOver(WebElement element) {
String code = "var fireOnThis = arguments[0];"
+ "var evObj = document.createEvent('MouseEvents');"
+ "evObj.initEvent( 'mouseover', true, true );"
+ "fireOnThis.dispatchEvent(evObj);";
((JavascriptExecutor) driver).executeScript(code, element);
}
and perform click on element
-
This is a good workable solution if you're stuck with using Selenium IDE. Wrapping the above JS into a 'runScript' block followed by a 'mouseDown' and 'mouseUp' on the
<div>
-esque button seemed to solve it for me.Priidu Neemre– Priidu Neemre2013年12月03日 09:55:39 +00:00Commented Dec 3, 2013 at 9:55
Andy, welcome to SQA.
In the Java API, the WebElement
interface has a click
method. I think the Python API uses the same method name.
-
1Hi, thanks for your reply. Yes, there is also a click() method in Python binding too. But so far I've only seen the click() method working fine with real link, button, radio/checkbox button elements. For those webapp's where link/button elements are implemented with only div or span tags, it seems the click() method does not work.Andy– Andy2012年03月27日 13:56:58 +00:00Commented Mar 27, 2012 at 13:56