【问题标题】:Click on element does not produce expected outcome using selenium webdriver使用 selenium webdriver 单击元素不会产生预期的结果
【发布时间】:2012-12-21 18:21:16
【问题描述】:

首先是规范,我正在用 java 编写 Selenium Webdriver 测试,用于主要用 ExtJS 编写并在 firefox 浏览器 v14 上运行的应用程序。有趣的是,Selenium 可以找到我想要单击的元素,但是单击似乎没有被执行,或者如果它正在执行,则不会发生所需的结果(出现弹出窗口)。我还在 Selenium IDE 中验证了我正在寻找的元素(我通过 Xpath 找到的 span 元素)存在,但在 Selenium IDE 中我遇到了同样的问题,无法点击它。

如果我手动点击按钮,弹出窗口会询问我要上传的文件。我还尝试了其他元素,例如跨度的父“按钮”元素和父“em”元素以及父“div”元素,但都没有运气。

让我感到沮丧的是,我已经为这个应用程序编写 Selenium 测试已经好几个星期了,并且总是能够使用这种方法来点击按钮,而对于这个特定的按钮,它不再起作用了。

        WebElement uploadButton = driver.findElement(By.xpath("//span[contains(text(), 'Upload Popup')]"));
    uploadButton.click();

编辑1:认为按钮本身的代码可能会有所帮助

<button id="filefield-1158-buttonEl-btnEl" class="x-btn-center" autocomplete="off" role="button" hidefocus="true" type="button" style="background-color: transparent;">

注意id是ExtJS创建的动态id

【问题讨论】:

    标签: java junit selenium-webdriver selenium-ide


    【解决方案1】:

    可能有很多原因。我建议您在测试运行时进行调试。

    可以在您的 Selenium Firefox 实例中安装 FireBug:

    File file = new File("firebug-1.8.1.xpi");
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.addExtension(file);
    firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
    
    WebDriver driver = new FirefoxDriver(firefoxProfile);
    

    我假设您可以通过 JUnit(在 Eclipse 等 IDE 中)启动您的测试。在调试模式下启动测试并在单击按钮之前设置断点。然后通过 FireBug 检查 html 代码。它可能会给你一个开始。

    另一种可能性是通过css类(By.className)或选择器(By.cssSelector)选择按钮:

    WebElement uploadButton = driver.findElement(By.className("x-btn-center"));
    uploadButton.click();
    

    如果页面上有多个按钮,您将不得不使用

    List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
    WebElement uploadButton = buttons.get(index);
    uploadButton.click();
    

    【讨论】:

    • 这是一个好主意,虽然现在我已经意识到 selenium webdriver 启动的 firefox 实例没有 firebug 并且简单地为此实例安装插件并不能成立(一旦我尝试第二次调试测试,萤火虫又消失了)。在解决这个小问题后,我会提供更新。
    • 我知道,一般selenium启动的firefox是没有firebug的,所以上面的代码是sn-p。
    • 您的第二种方法似乎是我应该尝试的方法,尽管目前我很难确定索引应该是什么。我不得不导入 java.util.List;对于 List,但 Eclipse IDE 仍然对 index 不满意
    • 要查找索引,请检查 firebug 或 chrome。我理解你的痛苦:) 我不得不编写一个测试来测试基于 Extjs 的数据网格。很难编辑单元格、选项卡单元格……
    【解决方案2】:

    试试这个:

         driver.findElements(By.xpath("//button[@class='x-btn-center']").click()
    

         driver.findElements(By.xpath("//*[@class='x-btn-center']").click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多