Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript xpath的行为就像单击按钮一样,但没有执行任何实际操作_Javascript_Java_Html_Selenium_Xpath - Fatal编程技术网

Javascript xpath的行为就像单击按钮一样,但没有执行任何实际操作

Javascript xpath的行为就像单击按钮一样,但没有执行任何实际操作,javascript,java,html,selenium,xpath,Javascript,Java,Html,Selenium,Xpath,我目前正在为一个web应用程序开发一个自动化测试套件。web应用程序非常挑剔。最近,我们有一个UI更新,所以我运行测试来测试我们的GUI和按钮,以及访问我们的XPath仍然在工作 万圣节前夜,我的剧本变得很滑稽~ 在这些套件中,我有一组两个测试:yesLogout和noLogout。yesLogout在您尝试注销时单击“是”按钮。noLogout在您尝试注销时单击“否”按钮 在测试脚本中运行noLogout时,脚本单击“No”,注销提示消失 当您在测试脚本中运行yesLogout时,脚本单击“

我目前正在为一个web应用程序开发一个自动化测试套件。web应用程序非常挑剔。最近,我们有一个UI更新,所以我运行测试来测试我们的GUI和按钮,以及访问我们的XPath仍然在工作

万圣节前夜,我的剧本变得很滑稽~

在这些套件中,我有一组两个测试:yesLogout和noLogout。yesLogout在您尝试注销时单击“是”按钮。noLogout在您尝试注销时单击“否”按钮

在测试脚本中运行noLogout时,脚本单击“No”,注销提示消失

当您在测试脚本中运行yesLogout时,脚本单击“是”按钮,但什么也没有发生。“Yes”(是)按钮会制作一个动画,就像单击了一样,但提示仍然存在

手动点击按钮就可以了

我将分享这两种方法(noLogout,yesLogout),因为它们实际上是相同的,它们只调用两个不同的XPath。为了回答这个问题,别忘了我正在测试——测试现在不是问题。假装我是个怪人,需要我的脚本来点击这个按钮

测试中使用的两个功能(极其简化):

属性文件中“是”按钮和“否”按钮的xpath:

yeslogout = //div[contains(@title,'Enter')]
nologout = //div[contains(@title,'Esc')]
以下是与此问题相关的HTML:

<div style="transform: translate(0px, 60px) scale(1, 1) rotate(0deg);">
  <div style="transform: translate(10px, 0px) scale(1, 1) rotate(0deg);">
    <div>
      <div tabindex="6" role="button" aria-label="Yes" title="Keyboard shortcut: Enter">
        <div>
        </div>
        <div tabindex="7" role="button" aria-label="No" title="Keyboard shortcut: Esc" style="transform: translate(163.5px, 0px) scale(1, 1) rotate(0deg);">
          <div>
          </div>
        </div>
      </div>
    </div>
显示一次又一次单击按钮,但没有实际功能

为了完整性和让人们理解这个问题,我重申一下,手动点击按钮效果非常好。输入快捷方式也是如此。Selenium
。单击()
命令可明显“工作”,但不执行按钮本身的功能

第二次编辑: 我将包括有关div的完整扩展HTML:

<div style="transform: translate(794px, 202px) scale(1, 1) rotate(0deg);" tabindex="5" role="dialog">
  <div>
    <div>
      <div>
        <div>
          <div>
            <canvas height="106" width="332" style="top: 0px; left: 0px; width: 332px; height: 106px;" />
          </div>
        </div>
        <div style="transform: translate(10px, 10px) scale(1, 1) rotate(0deg);">
          <div>
            <div style="transform: translate(6px, 6px) scale(1, 1) rotate(0deg);">
              <div style="font-family: franklin-gothic-urw; font-weight: 400; font-style: normal; font-size: 14px; opacity: 1; color: rgb(0, 0, 0);">Are you sure you want to logoff?</div>
            </div>
          </div>
        </div>
        <div style="transform: translate(0px, 60px) scale(1, 1) rotate(0deg);">
          <div style="transform: translate(10px, 0px) scale(1, 1) rotate(0deg);">
            <div>
              <div tabindex="6" role="button" aria-label="Yes" title="Keyboard shortcut: Enter">
                <div>
                  <div>
                    <div>
                      <div>
                        <div>
                          <canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
                        </div>
                      </div>
                      <div style="transform: translate(56px, 7px) scale(1, 1) rotate(0deg);">
                        <div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">Yes</div>
                      </div>
                      <div style="">
                        <div>
                          <div>
                            <canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
                          </div>
                        </div>
                        <div style="display: none;">
                          <div>
                            <canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
                          </div>
                          <div style="transform: translate(56px, 7px) scale(1, 1) rotate(0deg);">
                            <div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">Yes</div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div tabindex="7" role="button" aria-label="No" title="Keyboard shortcut: Esc" style="transform: translate(163.5px, 0px) scale(1, 1) rotate(0deg);">
                <div>
                  <div>
                    <div>
                      <div>
                        <div>
                          <canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
                        </div>
                      </div>
                      <div style="transform: translate(58px, 7px) scale(1, 1) rotate(0deg);">
                        <div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">No</div>
                      </div>
                      <div style="display: none;">
                        <div>
                          <div>
                            <canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
                          </div>
                        </div>
                        <div style="display: none;">
                          <div>
                            <canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
                          </div>
                          <div style="transform: translate(58px, 7px) scale(1, 1) rotate(0deg);">
                            <div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">No</div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

你确定要注销吗?
对
对
不
不

首先尝试使用不同的选项进行调试,如按键盘上的Enter键,以查看是否有效

WebElement.sendKeys(Keys.ENTER);
您可以尝试的其他选项是使用CSS查找元素或使用Java脚本方法单击元素

WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

参考资料:

我不知道这是否取决于您,但我会尝试为这些按钮添加一个
id
或一个更独特的属性(因为它们对应用程序很重要)

另一方面,您是否一定需要
actions.moveToElement()
之类的东西?你就不能单击元素吗?这可以减少任何意外行为:

WebElement yesLogout = driver.findElement(By.xpath("..."));
yesLogout.click();

最后一件事(为了更好地理解),我不确定我是否理解为什么要使用
div
作为按钮?为什么按钮位于(div)按钮内?这可能会导致一个问题。

有一些事情可能会导致这个问题,但如果您不在问题中添加相关的JavaScript,我无法确定。我假设在页面的JavaScript中,有一个函数绑定到click事件,它实际执行您期望的操作

可能是此事件绑定到“是”按钮的子级。在这种情况下,在浏览器中单击页面上的按钮可能会触发单击事件。但是,由于xpath的目标是父元素,selenium只会触发针对该元素的单击事件,因此不会触发其子元素上的任何事件。但是,如果动画附着到父对象,则仍可能触发该动画。这可以通过使xpath目标成为附加了click事件的特定元素来解决


不幸的是,如果JavaScript乱七八糟,那么查找事件所附加到的元素可能是一件痛苦的事情。一件可能对您有所帮助的事情是,如果您在ChromeDeveloperTools中检查一个元素,那么会有一个EventListeners选项卡。您可以看到哪些元素绑定了click事件,但如果它们使用jQuery,则不会显示具体的方法,而是其中一种jQuery方法。

我目前周末外出,但我将在周一的脚本中尝试。我从未听说过Javascript执行器。如果整个web应用程序都是用JavaScript编写的,那么学习所有ab是明智的吗
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
WebElement yesLogout = driver.findElement(By.xpath("..."));
yesLogout.click();