7

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.

3
  • 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
    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
    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
    Mar 29, 2012 at 9:52

3 Answers 3

4

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.

2
  • Great thanks @SteveCZ, it is working in Python with similar method as: <br/>ActionChains(driver).move_to_element(targetElement).click().perform()
    – Andy
    Mar 27, 2012 at 16:20
  • Java also has very similar API, helped me to figure that out, thanks!
    – ImtiazeA
    Mar 2, 2021 at 5:07
2
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

1
  • 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. Dec 3, 2013 at 9:55
0

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.

1
  • 1
    Hi, 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
    Mar 27, 2012 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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