1

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>
</a>

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

//*[@id="grdEmails"]/table/tbody/tr[1]/td[8]/div/a 

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?

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
IWebElement btnAction = driver.FindElement(By.ClassName("btn btn-sm blue"));
btnAction.Click();
2

5 Answers 5

1
  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

Try:

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

Or

driver.findElement(By.xpath("//a[contains(@class, 'btn') and contains(@class, 'btn-sm') and contains(@class, 'blue')]"));
1
  • 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
1

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
0

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:

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

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.

0

Try this:
Finding element by css-selector:

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

Finding element by xpath:

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

        driver.switchTo().activeElement();
        waitUntilElementVisibleByXpath(sChildXpath);
        WebElement button = driver.findElement(By.xpath(sChildXpath));
        button.click();
    }

// Checking for Visibility of element
    public void waitUntilElementVisibleByXpath(String sXpath) {
            WebDriverWait wait = new WebDriverWait(driver, 180);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(sXpath)));
        }

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.