Nightwatch.js 当被告知点击复选框标签时点击嵌套锚点

Nightwatch.js clicks on nested anchor when told to clicking on checkbox label

我有一个复选框,其中包含一个 link 的文本如下:

<div id="agreementDiv" class="cust-pad-bot1">
  <input id="inputAgreement" type="checkbox">
  <label for="inputAgreement">I have read and agree to the <a target="_blank" href="http://example.com/terms-and-conditions"><u>Terms &amp; Conditions</u></a></label>
</div>

(请注意,此片段只是实际实施的草图。CSS 类 已删除。)

使用 Nightwatch.js 我想自动选中复选框。在我的 UI 中,单击标签文本随后选中复选框。

browser
.useXpath()
.waitForElementPresent(`//*[@id="agreementDiv"]/label`, 10000)
.click(`//*[@id="agreementDiv"]/label`)

但是当我尝试使用 Nightwatch 执行此操作时,它会单击 link 并在新选项卡中打开 link。但是复选框没有被点击。使用 css/xpath 直接单击复选框也不起作用。如果有人能解释如何防止 nightwatch 点击子锚点,我将不胜感激。

谢谢。

您编写的代码是点击标签而不是复选框。更改 xpath。但是在使用 xpath 选择器之前使用方法 browser.useXpath();

    browser
.waitForElementPresent(`//*[@id="agreementDiv"]/input`, 10000)
.click(`//*[@id="agreementDiv"]/input`)

通过使用 css 选择器(nightwatch 中的默认选择器):

    browser
.waitForElementPresent(`#agreementDiv>input`, 10000)
.click(`#agreementDiv>input`)

这应该可以解决您的问题。如果您点击复选框,复选框将被选中。

我找到了一个使用 nightwatch-custom-commands-assertions 第三方模块 (https://github.com/maxgalbu/nightwatch-custom-commands-assertions). This module adds a set of custom nightwatch commands and among these there was jqueryClick function (https://github.com/maxgalbu/nightwatch-custom-commands-assertions) 的解决方案。我调用此函数提供复选框的 xpath,它运行良好。在复选框上使用默认 click 功能对我不起作用。