简体   繁体   English

Selenium-Webdriver可以找到元素,但未执行单击操作

[英]Selenium - webdriver can find element but click action is not performed

I am trying to automate functional testing of a web application using Selenium webdriver and Java. 我正在尝试使用Selenium webdriver和Java自动执行Web应用程序的功能测试。 In the AUT, there is a 'Submit' button defined by the following html code 在AUT中,有一个由以下html代码定义的“提交”按钮

<button id="submitbtn" class="btn btn-primary" type="submit">Submit</button>

I use the following command to click the button. 我使用以下命令单击该按钮。

driver.findElement(By.id("submitbtn")).click();

When I run the code, the webdriver can find the button but the click action is not performed (I can understand that webdriver can find the button because no exception is thrown and the I can see a selection on the button when the code is run). 当我运行代码时,Webdriver可以找到按钮,但是没有执行单击操作(我理解WebDriver可以找到按钮,因为不会引发异常,并且在运行代码时可以看到按钮上的选择) 。 I tried different waits 我尝试了不同的等待

new WebDriverWait(driver,60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn"));

but not getting any positive result. 但没有得到任何积极的结果。 If I use, 如果我用

Thread.sleep(3000);

it works fine (but I want to avoid this code). 它工作正常(但我想避免使用此代码)。 I tried all other types of waits and action class, 我尝试了所有其他类型的等待和行动课程,

Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.id("submitbtn"));
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn")));
action.click().perform();

but no luck. 但没有运气。 Is there any way to achieve this? 有什么办法可以做到这一点?

How about JavascriptExecutor? JavascriptExecutor呢?

WebElement element = driver.findElement(By.id("submitbtn"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

A submit() is an option driver.findElement(By.id("submitbtn")).submit(); driver.findElement(By.id("submitbtn")).submit(); submit()是选项driver.findElement(By.id("submitbtn")).submit(); . More information here 更多信息在这里

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

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