user3356141
user3356141

Reputation: 501

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?

Upvotes: 0

Views: 1027

Answers (1)

ddavison
ddavison

Reputation: 29032

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.

Upvotes: 2

Related Questions