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.

asked Mar 27, 2012 at 12:44
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. Commented 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? Commented 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! Commented 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.

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Mar 27, 2012 at 14:32
2
  • Great thanks @SteveCZ, it is working in Python with similar method as: <br/>ActionChains(driver).move_to_element(targetElement).click().perform() Commented Mar 27, 2012 at 16:20
  • Java also has very similar API, helped me to figure that out, thanks! Commented 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

answered Aug 14, 2012 at 7:15
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. Commented 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.

answered Mar 27, 2012 at 13:17
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. Commented Mar 27, 2012 at 13:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.