I am new in using selenium web driver. Can you help me which Element should I use? Here is the code.

<a class="btn btn-sm blue" data-toggle="dropdown">
  <i class="fa fa-gear"></i>
  <i class="fa fa-angle-down "></i>

I've already tried the class which is class="btn btn-sm blue" and its xpath


but none of this has been work.

I commit error whenever I run my script using C# language and it say "IllegalLocatorException". I've tried a lot of classes/xpath/id etc(which I know it doesn't appropriate to use just to work my script). Can somebody help me to solve this?

IWebElement btnAction = driver.FindElement(By.ClassName("btn btn-sm blue"));

5 Answers 5

  1. I think the By.ClassName does not support spaces in the selector.
  2. If the element is a new window, you first need to switchTo() the window, see this for an example


driver.findElement(By.cssSelector("a[class='btn btn-sm blue']"));


driver.findElement(By.xpath("//a[contains(@class, 'btn') and contains(@class, 'btn-sm') and contains(@class, 'blue')]"));
  • By.ClassName does support spaces but you must use "" instead of '' eg public static By scrollbarValue = By.xpath("//div[@class='nav-drop-down show']/div");
    – ECiurleo
    Dec 18, 2015 at 8:44

Try using below code

WebElement button = driver.findElement(By.xpath("//a[@class='btn btn-sm blue']"));
button.click(); // Click on button or any other action which you want to perform on button

Your class locator isn't working because that element has several different classes; they're separated by spaces in the html. If the best way to identify that element on your screen is by checking all the classes, then you can do with a css selector like this:


Notice the dots between the class names; that is telling webdriver to find an a element which has the class btn and has the class btn-sm and has the class blue.


Try this:
Finding element by css-selector:


Finding element by xpath:

driver.findElement(By.xpath(//a[@class="btn btn-sm blue"]));
// Modal popup button click pass the element xpath
    public void modalPopClickButtonByXpath(String sChildXpath) {

        WebElement button = driver.findElement(By.xpath(sChildXpath));

// Checking for Visibility of element
    public void waitUntilElementVisibleByXpath(String sXpath) {
            WebDriverWait wait = new WebDriverWait(driver, 180);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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