我有一些Selenium Chromedriver代码,单击某个按钮,然后几秒钟后,网页上出现了我需要捕获文本的项目(基本上是成功/失败响应)。我可以看到单击按钮的功能正常,但是脚本几乎在按下按钮后立即停止,但异常。我可以验证按钮单击是否起作用,因为脚本停止运行几秒钟后,要检查的元素出现在屏幕上。
例外是;
Caused by org.openqa.selenium.NoSuchElementException: no such element
这是有道理的,因为在发生此异常时,该元素不存在,并且将在几秒钟内不存在。
异常来自此命令;
myWaitVar.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@data-aura-class=\"forceActionsText\"]")));
将myWaitVar定义为;
myWaitVar = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(120))
.pollingEvery(Duration.ofSeconds(10))
.ignoring(org.openqa.selenium.NoSuchElementException.class);
忽略命令已从NoSuchElementException.class更改,因为我看到另一个Stackoverflow响应说它必须是org.openqa.selenium版本。
据我了解,该行最多应等待120秒,每10秒检查一次该元素是否出现。但是,测试结果显示它在0.076秒内完成。
如果我弄错了xpath,而实际上花了120秒,我会很高兴。但是,随着时间的推移,我相信问题可能在其他地方。
编辑:如果我添加了恰好7秒的线程睡眠,则可以正确识别该元素。延迟7秒,该元素出现在屏幕上,fluentwait通过,其余的测试也是如此。这表明元素定位器是准确的,没有iframe可以导航,没有其他DOM设置会阻止定位器在正确的时间工作。我也尝试过“存在”和“可见性”,但结果相同(我不能尝试“可单击”,因为它只是一个文本字段,不可单击)。
编辑2:这是此请求的调试响应。
[1567065113.459][INFO]: [ebbf7271cafbc1f7e2dba437ac32b004] COMMAND FindElement {
"using": "xpath",
"value": "//*[@data-aura-class=\"forceActionsText\"]"
}
[1567065113.459][INFO]: Waiting for pending navigations...
[1567065113.459][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=246) CB6F87B5FC515B000A21C08C843E210B {
"expression": "1"
}
[1567065113.464][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=246) CB6F87B5FC515B000A21C08C843E210B {
"result": {
"description": "1",
"type": "number",
"value": 1
}
}
[1567065113.464][INFO]: Done waiting for pending navigations. Status: ok
[1567065113.467][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=247) CB6F87B5FC515B000A21C08C843E210B {
"expression": "(function() { // Copyright (c) 2012 The Chromium Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style license that can be\n// found in the LICENSE file.\n\n/**\n * Enum f...",
"returnByValue": true
}
[1567065113.481][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=247) CB6F87B5FC515B000A21C08C843E210B {
"result": {
"type": "object",
"value": {
"status": 0,
"value": null
}
}
}
[1567065113.481][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=248) CB6F87B5FC515B000A21C08C843E210B {
"expression": "1"
}
[1567065113.483][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=248) CB6F87B5FC515B000A21C08C843E210B {
"result": {
"description": "1",
"type": "number",
"value": 1
}
}
[1567065113.483][INFO]: Waiting for pending navigations...
[1567065113.483][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=249) CB6F87B5FC515B000A21C08C843E210B {
"expression": "1"
}
[1567065113.483][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=249) CB6F87B5FC515B000A21C08C843E210B {
"result": {
"description": "1",
"type": "number",
"value": 1
}
}
[1567065113.483][INFO]: Done waiting for pending navigations. Status: ok
[1567065113.483][INFO]: [ebbf7271cafbc1f7e2dba437ac32b004] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"xpath","selector":"//*[@data-aura-class="forceActionsText"]"}
(Session info: chrome=76.0.3809.132)