Selenium:actions.moveToElement.click无效

ado*_*tyd 5 java selenium action

我不明白为什么这不起作用.我正在测试的Web应用程序有一个弹出框,单击按钮时会生成该框.此弹出框包含一个表,其每一行都是可单击的.我已经尝试了许多Actions,表行选择等实现,但没有任何工作.该元素对Selenium是可见的,它不会单击它.也没有抛出任何错误.

附加注意:我已经使用其他元素检查了Action方法,并且它可以工作,因此它必须是正在使用的选择器或它是如何看到它的.非常奇怪的行为.我已经在Firefox中使用Selenium IDE检查了它,weblement.click()将在CSS选择器上使用它.

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

助手班级:

public class Helper {

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

最奇怪的是,当我执行此实现时,它可以正常工作几次.然后我将Actions代码放入现已注释掉的Helper.scrollToElementAndClick()方法中,它就停止了工作.然后,当我回到这个实现时,它也没有工作!

我无法发布弹出窗口的图像,因为它会显示敏感信息,但这里是一些带有虚拟数据的popup示例HTML:

<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>
Run Code Online (Sandbox Code Playgroud)

Sig*_*hil 5

尝试将所有操作合并为一个操作,如下所示,然后重试。

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();

}
Run Code Online (Sandbox Code Playgroud)

您也可以尝试JavascriptExecuter如下所示:

((JavascriptExecutor)driver).executeScript("arguments[0].click();", selectAddress); 
Run Code Online (Sandbox Code Playgroud)

还要考虑td包含一些可以点击的其他元素(输入、链接)的可能性(我不知道你的 html 代码)。