I have some Selenium Chromedriver code that is clicking on a button, then several seconds later an item appears on the web page that I need to capture the text (basically a success/fail response). I can see the button click is working, but the script stops with an exception almost immediately after the button is pressed. I can verify the button click has worked because a few seconds after the script has stopped, the element to be checked appears on screen.
The exception is;
Caused by org.openqa.selenium.NoSuchElementException: no such element
This makes sense because at the time this exception occurs the element is not there, and it won't be there for several seconds.
The exception comes from this command;
myWaitVar.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@data-aura-class=\"forceActionsText\"]")));
With myWaitVar defined as;
myWaitVar = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(120))
.pollingEvery(Duration.ofSeconds(10))
.ignoring(org.openqa.selenium.NoSuchElementException.class);
The ignoring command was changed from NoSuchElementException.class because I saw another Stackoverflow response saying that it has to be the org.openqa.selenium version.
From what I understand, the line should wait for up to 120 seconds, checking every 10 seconds for the element to appear. However, the test result shows that it completes in 0.076 seconds.
I would be happy if I had got the xpath wrong and it actually took 120 seconds to break. But with it breaking in just over a second I believe the issue could be somewhere else.
Edit: The element is identified correctly if I add a thread sleep of exactly 7 seconds. With a 7 second delay, the element appears on screen, the fluentwait passes and so does the rest of the test. This suggests that the element locator is accurate, there is no iframe to navigate, no other DOM setting that prevents the locator from working at the right time. I have also tried 'presence of' as well as 'visibility of' with the same result (I can't try 'is clickable' as it's just a text field and not clickable).
Edit 2: This is the debugging response for this request.
[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)
> presenceOfAllElementsLocatedBy( final By locator) https://github.com/SeleniumHQ/selenium/blob/07a18746ff756e90fd79ef253a328bd7dfa9e6dc/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java#L324 If its not happening here then we can write workaround by extending the ExpectedConditions
– Rahul L Aug 28 '19 at 15:47