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
3
  • <a href="#"> <button id="listbtn" class="btn btn-info" type="button" name="listbtn">Create</button> </a>
    – josh test
    Dec 23, 2015 at 11:42
  • 3
    Can you update the exception thrown (full stack trace) 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
    – josh test
    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']"));
5
  • ok .. will try the above selector
    – josh test
    Dec 24, 2015 at 6:52
  • Let me know if it works or not.
    – Robben
    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
    – Julian
    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.
    – Robben
    Jan 22, 2016 at 21:58
  • If you prefer a less is more approach see my answer sqa.stackexchange.com/a/27854/8992 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.

0

I prefer the shorter form

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.