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
-
<a href="#"> <button id="listbtn" class="btn btn-info" type="button" name="listbtn">Create</button> </a>josh test– josh test2015年12月23日 11:42:00 +00:00Commented Dec 23, 2015 at 11:42
-
3Can you update the exception thrown (full stack trace)demouser123– demouser1232015年12月23日 12:06:47 +00:00Commented 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.htmljosh test– josh test2015年12月24日 06:51:50 +00:00Commented Dec 24, 2015 at 6:51
3 Answers 3
I use CSS selectors, thus it would be
driver.findElement(By.cssSelector("a button[id='listbtn']"));
answered Dec 23, 2015 at 16:30
-
ok .. will try the above selectorjosh test– josh test2015年12月24日 06:52:36 +00:00Commented Dec 24, 2015 at 6:52
-
Let me know if it works or not.Robben– Robben2015年12月28日 14:28:11 +00:00Commented 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 itselfJulian– Julian2016年01月22日 19:16:51 +00:00Commented 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– Robben2016年01月22日 21:58:39 +00:00Commented Jan 22, 2016 at 21:58
-
If you prefer a less is more approach see my answer sqa.stackexchange.com/a/27854/8992Michael Durrant– Michael Durrant2017年06月17日 11:10:44 +00:00Commented Jun 17, 2017 at 11:10
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
I prefer the shorter form
driver.findElement(By.css("a button#listbtn"));
answered Jun 17, 2017 at 10:56
default