Question

I was struggling with a popup dialog in a website I'm automating, for some reason it wouldn't click a button in the dialog but interacted fine with everything else fine. It was also identifying the button without issue and I could see when watching it run that it did seem to click the button as the colour changed, so I was a bit stuck.

My code originally looked pretty obvious: -

WebElement targetElement = waitforElement(threadSesh);
targetElement.click();

But when I changed it to the following all seemed to work absolutely fine!

WebElement targetElement = waitforElement(threadSesh);
Actions action = new Actions(threadSesh.driver);
action.click(targetElement);
action.perform();

Can anyone tell me why one method doesn't work while the other works fine? Also, whether it helps or not, the site is built using twitter bootstrap if that makes any difference but the button itself was pretty basic, like so.

<div class="modal hide fade in" style="width: 481px; margin-left: -241px; padding: 15px; display: block;" role="dialog" data-backdrop="static" data-keyboard="false" aria-hidden="false">
...
<div class="modal-footer">
<a id="save-EditDetails" class="btn btn-small btn-info" type="button" style="margin-right: 7px; padding-left: 15px; padding-right: 15px;" data-bind="click: ok" href="#">Save</a>
</div>
</div>
Was it helpful?

Solution

I am not sure about your specific case, but there are several differences between WebElement click method and Actions click method. Actions' click is a lot dumber, it pretty much just sends the click event to the element (location) that you pass in. It does not care about the element, it just does the click and moves forward whereas webelement click is a blocking call (not always, check the references) and it also has preconditions like the WebElement to be clicked must be visible. Also, webElements' click is a void method, actions click returns reference to the Actions you are using. For more information check here and here.


edit. Looking at the markup you posted, and it can be totally wrong as I am not a boss on bootstrap CSS, the modal hide fade in and especially the fade in part there looks suspicious. Are you sure that when you send the webelement.click(), your element is in clickable state? What happens? Nothing? Then again, if the actions click is reliably working, why not just go with it, I mean, if something works, why fix it?

OTHER TIPS

Just testing similar scenario.

First with Actions click:

actions.moveToElement(driver.findElement(By.xpath("//*[@id='relevantJobsAndCareerUpdates_1']"))).click().perform()

And second one with WebElement click:

driver.findElement(By.xpath("//*[@id='relevantJobsAndCareerUpdates_1']"))).click()

The second one does not work. It gives the error message:

Root cause: org.openqa.selenium.WebDriverException: unknown error: Element <input data-val-required="Information required.
" id="relevantJobsAndCareerUpdates_1" name="OptiInEmailJobsAndCarrerRelated" type="radio" value="1"> is not clickable at point (307, 24). Other element would receive the click: <label for="relevantJobsAndCareerUpdates_1" class="control-label">...</label>

This is the similar problem as previous. Using Actions solve this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top