Java Selenium:actions.moveToElement.click not working
我不明白这为什么不起作用。我正在测试的Web应用程序有一个弹出框,它是在单击按钮时生成的。此弹出框包含一个表格,每一行都可以单击。我已经尝试了许多操作、表行选择等的实现,但都不起作用。元素对Selenium是可见的,它只是不会单击它。也没有抛出错误 附加说明:我已经用其他元素检查了Action方法,它可以工作,所以它必须是正在使用的选择器或者它是如何看到它的。非常奇怪的行为。我还使用Selenium IDE和WebElement在Firefox中进行了检查。单击()将在CSS选择器上使用它Java Selenium:actions.moveToElement.click not working,java,selenium,action,Java,Selenium,Action,我不明白这为什么不起作用。我正在测试的Web应用程序有一个弹出框,它是在单击按钮时生成的。此弹出框包含一个表格,每一行都可以单击。我已经尝试了许多操作、表行选择等的实现,但都不起作用。元素对Selenium是可见的,它只是不会单击它。也没有抛出错误 附加说明:我已经用其他元素检查了Action方法,它可以工作,所以它必须是正在使用的选择器或者它是如何看到它的。非常奇怪的行为。我还使用Selenium IDE和WebElement在Firefox中进行了检查。单击()将在CSS选择器上使用它 pu
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();
}
最奇怪的是,当我执行这个实现时,它工作正常了好几次。然后我将操作代码放入现在注释掉的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>
关闭
有效地址:
邮政编码:不适用
白杨树路2号
金赛路
尝试将所有操作合并为一个操作,如下所示,然后重试
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代码)。
因为这是一个弹出框,尝试添加一个元素来检查元素的存在,然后用Action类执行代码。@ SUBH尝试并添加一个验证PrimTLN来确定。根据显式等待,元素存在且可单击,但单击未注册。你能添加相关的HTML代码片段和弹出窗口的图像吗?此外,如果是一个你正在自动化的公共站点,你能提供URL吗?谢谢你的输入。我在上面尝试了不同版本的代码,但仍然不起作用。元素可见且可单击(通过Selenium.isClickable()
验证),但单击未被注册。我甚至在点击操作之后放了一个println,它被打印到控制台上,所以还有其他错误。我尝试了一个JavascriptExecutor
,它没有问题<代码>JavascriptExecutor executor=(JavascriptExecutor)驱动程序;executor.executeScript(“参数[0]。单击();”,webElement)代码>感谢您的输入。您能解释一下为什么这是有效的,而不是操作
解决方案吗?如果不了解整个应用程序,我无法确定这一点。在我的应用程序中,我遇到了类似的问题,在这些问题上我找不到有效的原因。大多数情况下,javascript的点击是有效的,因为无法通过正常的方式访问元素。。也许这件事被掩盖了