Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 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 '12 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 '12 at 9:52

3 Answers 3

up vote 0 down vote accepted

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.

share|improve this answer
    
Great thanks @SteveCZ, it is working in Python with similar method as: <br/>ActionChains(driver).move_to_element(targetElement).click().perform() –  Andy Mar 27 '12 at 16:20

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.

share|improve this answer
    
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 '12 at 13:56
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

share|improve this answer
    
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 Dec 3 '13 at 9:55

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.