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

标签 javascript java html selenium xpath

我目前正在为 Web 应用程序开发自动化测试套件。 Web 应用程序确实很挑剔。最近,我们进行了 UI 更新,因此我运行了测试来测试我们的 GUI 和按钮,以及访问我们的 xpath 是否仍在工作。

万圣节前夜,我的剧本变得真实恐怖~

Ghosts probably in my script

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

当您在测试脚本中运行 noLogout 时,脚本会单击“否”,注销提示就会消失。

当您在测试脚本中运行 yesLogout 时,脚本单击"is"按钮,但没有任何反应。 "is"按钮会产生动画,就像被单击一样,但提示仍保留在那里。

手动单击按钮效果很好。

我将分享这两个方法(noLogout、yesLogout),因为它们实际上是相同的,只是调用两个不同的 xpath。为了这个问题,请忘记我正在测试 - 测试现在不是问题。假装我是一个怪人,只需要我的脚本来单击此按钮。

用于测试的两个函数(极其简化):

public class HiStackOverflow {

    public class yesLogout {
        public static void run(WebDriver driver) throws FileNotFoundException {
            Properties prop = new Properties();
            InputStream selectproductsidebar;

            selectproductsidebar = new FileInputStream(
                "C:/PATH/TO/PROPERTIES");
            try {
                prop.load(selectproductsidebar);
            } catch (IOException e) {
                e.printStackTrace();
            }


            WebElement yesLogout = driver.findElement(By.xpath(prop.getProperty("yeslogout")));
            Actions actions = new Actions(driver);
            actions.moveToElement(yesLogout).build().perform();
            yesLogout.click();
        }
    }

    public class noLogout {
        public static void run(WebDriver driver) throws FileNotFoundException {
            Properties prop = new Properties();
            InputStream selectproductsidebar;

            selectproductsidebar = new FileInputStream(
                "C:/PATH/TO/PROPERTIES");
            try {
                prop.load(selectproductsidebar);
            } catch (IOException e) {
                e.printStackTrace();
            }


            WebElement noLogout = driver.findElement(By.xpath(prop.getProperty("nologout")));
            Actions actions = new Actions(driver);
            actions.moveToElement(noLogout).build().perform();
            noLogout.click();
        }

    }
}

属性文件中"is"按钮和“否”按钮的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>

tabindex="6"是按钮。 tabindex="7"是没有按钮。两者之间有一个小小的区别(没有按钮有某种转换:附加了翻译魔法),但就我而言,它不应该表现得如此奇怪。

再一次,实际问题 - 运行 yesLogout 方法找到、悬停并单击"is"按钮,但没有任何反应。手动单击"is"按钮效果很好。

我可以再扩展一些<div>如果需要的话,可以在 html 中添加 s ,但我真的不认为它会显示太多(只是更多格式和 <canvas> 的东西)。我真的很想解释一下正在发生的事情。提前致谢。

编辑: 我尝试使用 JavascriptExecutor 来单击该元素,但遗憾的是,它不起作用。这整个周末都让我绞尽脑汁。

甚至做一些疯狂的事情,比如:

WebElement yesLogout = driver.findElement(By.xpath(prop.getProperty("yeslogout")));
        Actions actions = new Actions(driver);
        actions.moveToElement(yesLogout, 10, 10).build().perform();
        yesLogout.click();
        yesLogout.click();
        yesLogout.click();
        yesLogout.click();
        yesLogout.click();
        yesLogout.click();

显示按钮被一遍又一遍地点击,但没有实际功能。

为了完整起见并试图让人们理解这个问题,我将重申,手动单击按钮效果非常好。 Enter 快捷键也是如此。 Selenium .click()命令VISIBLY“工作”,但不执行按钮本身的功能。

第二次编辑: 我将包含相关 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);

引用:How to click an element in Selenium WebDriver using JavaScript

关于javascript - xpath 的行为就像单击按钮一样,但没有执行任何实际操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443781/

相关文章:

html - ul在nav中的定位

javascript - 在 Bootstrap 3 中,如何将下拉列表中的列表项设置为一级菜单项,并且仅在 xs 屏幕尺寸上设置?

javascript - 处理数据的多个步骤,JS/Angular 有什么好的模式?

Java网络: how to simulate two different computers?

java - 如何使用 Java 在任何操作系统中创建隐藏文件

java - 从 Web url 检索完整页面源

javascript - 如何选择所有父复选框?

javascript - 使用 handlebar js 使用 data() 将 json 值存储到 li

javascript - Javascript 函数的意外结果

javascript - lodash 中是否有比较两个对象数组,如果结果相同则不会显示