Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: actions.moveToElement.click not working

I can't understand why this isn't working. The Web app that I'm testing has a pop up box that is generated on clicking a button. This popup box contains a table, each row of which is clickable. I have tried numerous implementations of Actions, table row selection etc. but nothing is working. The element is visible to Selenium, it just won't click it. No error is being thrown either.

ADDITIONAL NOTE: I've checked the Action method with other elements and it works so it has to be the selector being used or how it is seeing it. Very strange behaviour. I've also checked it in Firefox with the Selenium IDE and weblement.click() will work on the CSS selector with that.

public class ContactDetails {
    WebDriver driverInstance;

public ContactDetails(WebDriver driver){
    this.driverInstance = driver;
}

public void enterContactDetails(){

    //Other code here...

    By validAddress = By.cssSelector("#customerAddress > tbody > tr:nth-child(1) > td");
        //Validate that the element is visible. Definitely working as intended because I use it elsewhere in the code successfully.
        if (Helper.checkElementVisible(driverInstance, validAddress)){ 
            //if visible:
            WebElement selectAddress = driverInstance.findElement(validAddress);
            //Helper.scrollToElementAndClick(driverInstance, selectAddress);
            Actions actions = new Actions(driverInstance);
            actions.moveToElement(selectAddress).click().perform();
        }
    }
}

Helper Class:

public class Helper {

    public static void scrollToElementAndClick(WebDriver driver, WebElement webelement){
    Actions actions = new Actions(driver);
    actions.moveToElement(webelement).click().perform();
}

The strangest thing is that it worked ok a couple of times when I did this implementation. I then put the Actions code into the now commented out Helper.scrollToElementAndClick() method and it stopped working. Then when I went back to this implementation it wasn't working either!

I can't post an image of the popup because it would reveal sensitive info but here is some sample HTML of popup with dummy data:

<div class="someDiv" tabindex="-1" role="dialog" aria-labelledby="ui-1"
style="height: auto; width: 600px; top: 175px; left: 364px; display: block;">
  <div class="anotherDiv">
     <span id="ui-1" class="ui-title"></span> 
     <button class="ui-title-close" role="button" aria-disabled="false" title="close"> 
       <span>close</span>
     </button>
  </div>
  <div id="validateCustomerAddress" class="ui-content" style="width: auto; min-height: 0px; max height: none; height: 230px;">
<h2 class="aSection" style="color:#666666">Valid Addresses:</h2>
<table id="customerAddress">
  <tbody>
    <tr>
      <td>ZIP CODE: N/A</td>
    </tr>
    <tr>
      <td>2 POPLAR WAY</td>
    </tr>
    <tr>
      <td>KINSEY DRIVE</td>
    </tr>
  </tbody>
</table>
</div>
</div>
like image 624
adohertyd Avatar asked Dec 09 '14 18:12

adohertyd


1 Answers

Try combining all the action into one action as shown below and try again.

public class Helper {

public static void scrollToElementAndClick(WebDriver driver, WebElement webelement){
Actions actions = new Actions(driver);
actions.moveToElement(webelement).click();

action = action.build;
action.perform();

}

Also you can try JavascriptExecuter as shown below:

((JavascriptExecutor)driver).executeScript("arguments[0].click();", selectAddress); 

Also consider the possibility of the td containing some other element (input, link) which can be clicked(I dont know your html code).

Hope this helps you.

like image 138
Sighil Avatar answered Oct 21 '22 02:10

Sighil