1

The following HTML code is for clicking a button

<a href="#">
 <button id="listbtn" class="btn btn-info" type="button" name="listbtn">Create</button>
</a>

Have tried the following locators:

// driver.findElement(By.linkText("Create")).click();
// driver.findElement(By.name("listbtn")).submit();
//driver.findElement(By.id("listbtn")).click();
//driver.findElement(By.className("btn btn-info")).click();
//driver.findElement(By.xpath("//*[@id='listbtn']")).click();
//driver.findElement(By.xpath(("td[class='btn btn-info'][value='Create']"))).click();
//driver.findElement(By.linkText("Cancel")).click();
//WebElement element=driver.findElement(By.xpath("//*[@id='listbtn']"));
//driver.findElement(By.xpath("//a[@href='listbtn']")).click();

But when clicking run, it shows invalid locator -

The following is the error exception Exception in thread "main" org.openqa.selenium.InvalidSelectorException:
The given selector btn btn-info is either invalid or does not result in a WebElement. 
The following error occurred: InvalidSelectorError: Compound class names not permitted
Command duration or timeout: 24 milliseconds For documentation on this error, please visit: 
seleniumhq.org/exceptions/invalid_selector_exception.html
Michael Durrant
25.3k3 gold badges42 silver badges114 bronze badges
asked Dec 23, 2015 at 11:40
3
  • <a href="#"> <button id="listbtn" class="btn btn-info" type="button" name="listbtn">Create</button> </a> Commented Dec 23, 2015 at 11:42
  • 3
    Can you update the exception thrown (full stack trace) Commented Dec 23, 2015 at 12:06
  • The following is the error exception Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector btn btn-info is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted Command duration or timeout: 24 milliseconds For documentation on this error, please visit: seleniumhq.org/exceptions/invalid_selector_exception.html Commented Dec 24, 2015 at 6:51

3 Answers 3

3

I use CSS selectors, thus it would be

driver.findElement(By.cssSelector("a button[id='listbtn']"));
answered Dec 23, 2015 at 16:30
5
  • ok .. will try the above selector Commented Dec 24, 2015 at 6:52
  • Let me know if it works or not. Commented Dec 28, 2015 at 14:28
  • You can make it a bit less clunky looking if you use id shorthand: "a button#listbtn". Since ids tend to be unique on a page, chances are you will be able to get away with the css selector "#listbtn" by itself Commented Jan 22, 2016 at 19:16
  • @JulianCleary That's true Julian. I write my selectors that way because I like it spelled out - just a personal preference. Commented Jan 22, 2016 at 21:58
  • If you prefer a less is more approach see my answer sqa.stackexchange.com/a/27854/8992 Commented Jun 17, 2017 at 11:10
1

The error you get is because compound classes are not permitted inside by.className locators.

The By.id locator should be the best and the fastest option here:

driver.findElement(By.id("listbtn"));

You can also construct a CSS selector:

driver.findElement(By.cssSelector("a button#listbtn"));

Note the use of # shortcut.

answered Jun 17, 2017 at 2:59
0

I prefer the shorter form

driver.findElement(By.css("a button#listbtn"));
answered Jun 17, 2017 at 10:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.