无法使用Java和Selenium Webdriver单击按钮

时间:2014-03-13 13:00:32

标签: java selenium webdriver

我需要测试的表单上有以下两个按钮:

 <div class="ef-buttons">

 <button value="next" name="action" type="submit">
  Continue
 </button>

 <button id="modify_button" value="previous" name="action" type="submit">
  Go Back
 </button>

 </div>

我想点击继续按钮,为此我编写了下一段代码:

    By chain = new ByChained(By.className("ef-buttons"),(By.xpath("//*[@value='next']")));
    driver.findElement(chain).click();

但是,每次我收到消息时都找不到元素。我做错了什么?

1 个答案:

答案 0 :(得分:2)

我建议合并你的By,并使用CSS。它更快更容易。这就是你选择元素的方式:

driver.findElement(By.cssSelector("div.ef-buttons button[name='action']")).click();

仅供参考,最好使用name属性而不是value,因为name更加独特。