简体   繁体   English

无法在Java Selenium中单击按钮?

[英]Not able to click on button in java selenium?

HTML code: HTML代码:

<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. 您可以尝试通过cssSelector来获取它。 As far as I remember by className is only for one class. 据我记得, className仅适用于一个类。

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. 通过网络驱动程序执行单击有时会出现意外行为,如果无法正常运行,请使用JavascriptExecutor类的替代方法执行此操作。 Its always preferable to use click() method of the WebElement . 始终最好使用WebElement click()方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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