2

HTML code:

<button type="button" class="btn btn-main dropdown-toggle" dropdown-toggle="" aria-haspopup="true" aria-expanded="false">create
                <span class="icon-dir-down"></span>
                <span class="sr-only">Toggle Dropdown</span>
            </button>

Using class name I tried to click on button but am not able to click.

I tried the following code:

driver.findElement(By.className("btn btn-main dropdown-toggle")).click();
3
  • Are there other Web Elements in your project that you are finding via className? And, are they working? Commented Feb 2, 2016 at 18:37
  • @Brian No..help me please i don't know how to do it ...please Commented Feb 2, 2016 at 18:38
  • selenium contributor here. check out this issue filed on our repository. you are misusing className. @PeterRasmussen's answer is the best way to accomplish it. Commented Feb 2, 2016 at 18:43

2 Answers 2

4

You can try and get it by cssSelector instead. As far as I remember by className is only for one class.

driver.findElement(By.cssSelector(".btn.btn-main.dropdown-toggle")).click();
Sign up to request clarification or add additional context in comments.

3 Comments

OP, Peter beat me to it. I was going to suggest finding it by the cssSelector as well. Give it a shot.
see my comment above. @raghavendrat if you wanted to know why.. myself and luke (contributors to selenium) discuss the reasons
@Brian yes sure... I should wait 3 minutes to accept this answer
1

Executing a click via webdriver has sometime unexpected behaviors.If its not working then alternate way JavascriptExecutor class to do this. Its always preferable to use click() method of the WebElement.

WebElement element = driver.findElement(By.cssSelector(".btn.btn-main.dropdown-toggle"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Comments

Your Answer

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.