java - WebElement在浏览器中使用cssSelector、xpath可定位,虽然不为null,但为NotPresent、NotClickable、NotEnabled

标签 java angular selenium selenium-webdriver selenium-chromedriver

这可能是 Selenium 问题或 Angular 问题。我不知道.. 我有一个带有以下 html 代码的按钮元素。不过,这是一个 Angular 网站。

<div class="col-xs-12 col-sm-3 col-sm-push-6 col-lg-3" css="1">
<button class="btn btn-secondary pull-right" style="background-color: #00A6CA; border-color:#00A6CA;" type="button">
<span aria-hidden="true" class="fa tufa-plus-circle-o" style=""></span>
NEW EXTRACT 
</button>
</div>

我使用 xpath 作为 //button[contains(text(),'NEW EXTRACT ')] 和 css 选择器作为 'button.btn.btn-secondary.pull-right'。两者都很好地在浏览器控制台中一对一(而不是多个)地识别了该按钮。当我在页面对象代码中使用它们中的任何一个时,例如......

System.out.println("@@@@@@@@@@@ is NOT null> "+(newExtractBtn != null));
System.out.println("@@@@@@@@@@@ to string> "+newExtractBtn.toString());
System.out.println("@@@@@@@@@@@ is present> "+newExtractBtn.isPresent());
Thread.sleep(10000);
System.out.println("@@@@@@@@@@@ Thread sleep for 10sec");
newExtractBtn.waitUntilEnabled();
System.out.println("@@@@@@@@@@@ waited until enabled");
newExtractBtn.waitUntilPresent();

输出为...

I am getting out put as 
@@@@@@@@@@@ is NOT null> true
@@@@@@@@@@@ to string> ExtractExecutionHistoryPage.newExtractBtn
@@@@@@@@@@@ is present> false
@@@@@@@@@@@ Thread sleep for 10sec

一旦到达 webelement 对象**newExtractBtn**,它就会抛出以下消息..

> net.thucydides.core.webdriver.exceptions.ElementShouldBeEnabledException: Expected enabled element was not enabled
> Caused by: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for ExtractExecutionHistoryPage.newExtractBtn to be enabled (tried for 5 second(s) with 100 milliseconds interval)

为什么 web 元素 newExtractBtn 可以通过 xpath, css 在浏览器中定位,并且在网页上可见并且可点击,但在运行时不存在且不可点击?

最佳答案

有两件事...

1)您正在做出假设。由于测试由于按钮未“启用”而失败,因此它从不测试它是否存在。由于 css 和 xpath 找到了它,它就存在了,但是您的代码从未达到这一点。

2) 按钮存在并不意味着它已启用。 JavaScript 可以禁用按钮,直到满足某些条件。在输入并验证所有值之前,在启动页面时禁用“提交”按钮的情况并不罕见。

关于java - WebElement在浏览器中使用cssSelector、xpath可定位,虽然不为null,但为NotPresent、NotClickable、NotEnabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775849/

相关文章:

javascript - Ionic 4 ion-slides第二次忽略slideOptions

django - 在 Mac OSX Mavericks 上将 selenium 与 Psycopg2、Postgres 一起使用时出现 webdriver 异常

java - Selenium WebDriver - 更改 Linux 上的浏览​​器语言

java - 如何在断言中将值写入字符串

java - Spring : can't persist entity annotated with @JsonIgnore

java - 如何写入/读取已锁定的文件?

angular - 在 NgFor 循环中与 NgIf

java - 快速排序降序不升序排序

java - Swing 中的连续工具提示更新

angular - 如何在自定义输入上触发 ngModelChange?