简体   繁体   中英

Cannot click a button using Java and Selenium Webdriver

I have the following two buttons on a form I need to test:

 <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>

I want to click the Continue Button, for which I wrote the next piece of code:

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

However, everytime I get the message Cannot locate an element. What am I doing wrong?

I'd recommend consolidating your By , and just using CSS. It's faster and easier. This is how you'd select your element:

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

FYI, it's better practice to use the name attribute over value since name is more unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM