简体   繁体   中英

Not able to click on button in java selenium?

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();

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();

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM