Selenium:actions.moveToElement.click无法正常工作

6
我不明白为什么这个操作不起作用。我正在测试的Web应用程序有一个弹出框,点击按钮后会生成。此弹出框包含一个表格,其中每行都可点击。我尝试了多种实现方法,包括操作、表格行选择等,但都无效。该元素对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();
        }
    }
}

辅助类:

public class Helper {

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

最奇怪的是,我使用这种实现方式时几次都能正常运行。然后我将Actions代码放入现在被注释掉的Helper.scrollToElementAndClick()方法中,结果它停止工作了。当我回到这种实现方式时,它也不能工作了!

我不能发布弹出窗口的图像,因为那会泄露敏感信息,但以下是带有虚假数据的弹出窗口示例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>

由于这是一个弹出框,建议先添加显式等待来检查元素是否存在,然后再使用Action类执行代码。 - Subh
@Subh 尝试了那个,并添加了一个验证的 println 以确保。根据显式等待,该元素是存在且可点击的,但是点击无效。 - adohertyd
好的。您能否添加相关的HTML代码片段和弹出窗口的图像?此外,如果您正在自动化公共站点,请提供URL。 - Subh
1个回答

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

您可以尝试使用以下代码中展示的JavascriptExecuter

}

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

还要考虑 td 可能包含其他可点击的元素(如输入框、链接),具体取决于你的 HTML 代码。

希望这可以帮助你。


感谢您的输入。我尝试了上面不同版本的代码,但仍然无法正常工作。该元素是可见且可以点击(通过Selenium的.isClickable()验证),但单击操作没有被注册。我甚至在单击操作之后放置了println语句,并将其打印到控制台,因此还有其他问题。 - adohertyd
1
我尝试了 JavascriptExecutor,没有问题。JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", webElement); 感谢您的回复。您能解释一下为什么这个方法可行而 Actions 的解决方案不行吗? - adohertyd
1
如果没有看到你的整个应用程序,我无法确定这一点。在我的应用程序中,我也遇到了类似的操作问题,但我无法找到有效的原因。 - StrikerVillain
大多数情况下,JavaScript 点击操作有效,因为元素无法通过正常方式访问...也许该元素被覆盖了。 - wutzebaer
@adohertyd 这完全取决于元素的可达性。我曾经有一个 switch 语句,在其中如果你点击了 switch 语句,就会出现错误,但是如果你点击了 switch 元素内部的文本元素,则不会出现错误。这取决于 js 点击事件监听器所附加的位置。如果滚动不起作用,我曾经遇到过这个问题,即布局覆盖了部分滚动内容,并且操作足够滚动,以便该元素在屏幕上显示,如果它没有被覆盖。 - Darkproduct

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接