1

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)
Slimy43
  • 341
  • 4
  • 17
  • I've run into this issue in other bindings as well. Ended up just working around it, would be nice to see a real solution :) – mrfreester Aug 28 '19 at 15:07
  • Can you check with ExpectedCondition> 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
  • Looks like https://github.com/SeleniumHQ/selenium/blob/07a18746ff756e90fd79ef253a328bd7dfa9e6dc/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java#L898 line is culprit – Rahul L Aug 28 '19 at 15:49
  • @RahulL, I can understand where the exception is coming from in the ExpectedConditions, and I believe the exception is being correctly thrown as the element isn't visible. It's the 'ignoring' bit that I don't fully understand, why is it not being ignored? It did occur to me that I could put the entire code in try/catch block, add a finite loop and deal with the exception myself, but that seems to defeat the object of having an 'ignoring' section. – Slimy43 Aug 28 '19 at 19:48
  • @mrfreester, could you give me an example of a workaround? I did think of a try/catch block to do the same process, but I have a feeling this won't be the last time this happens and I'd prefer something reusable. – Slimy43 Aug 28 '19 at 19:51
  • @Slimy43 not as familiar with python, but in .NET we did some form of a try catch as an anonymous method inside of the `until` parameter if I remember correctly. – mrfreester Aug 28 '19 at 20:13
  • @DebanjanB none of those questions/answers seem to address this question as suggested in the duplicate flag. – mrfreester Aug 28 '19 at 21:48
  • @mrfreester To address _NoSuchElementException_ you have to come out of `presenceOfElementLocated()`. Clearly the application is JS build. Either you need to wait for the element's visibility through `visibilityOfElementLocated()` or you need to wait for the element's interactability through `elementToBeClickable()`. Having said all these 1) Are you sure failed _ExpectedConditions_ may raise _NoSuchElementException_? 2) Shouldn't it be _TimeoutException_? 3) Are you sure the _locator_ is correct? 4) Did you test the locator with respect to the DOM? Can update the question with these min info? – undetected Selenium Aug 28 '19 at 22:00
  • @DebanjanB I'm not sure I follow. 1) OP stated that `NoSuchElementException` is being thrown, so that appears to be the exception that is being thrown. 2) Yes, if it was working properly, but that's the point of this question. 3) Perhaps not, but that still should lead to the `NoSuchElementException` being ignored properly which is the point of this question. Basically, why is `NoSuchElementException` being thrown when `FluentWait` is set to ignore it? – mrfreester Aug 28 '19 at 22:07
  • @mrfreester You haven't answered _3) Are you sure the locator is correct? 4) Did you test the locator with respect to the DOM?_ and you can't answer the questions until and unless OP provides the relevant HTML which is the basic necessity to construct a canonical answer. – undetected Selenium Aug 28 '19 at 22:10
  • @DebanjanB I think you're missing the point I was trying to make with my answer to number 3. This isn't about the selector properly finding an element, it's about why `NoSuchElement` Exception isn't being ignored. Even if a completely incorrect selector is given that doesn't change this question, as the exception should still be ignored. This question is about this line of code `.ignoring(org.openqa.selenium.NoSuchElementException.class)` and why that doesn't appear to work. It's not about the selector. – mrfreester Aug 28 '19 at 22:13
  • @mrfreester First of all _FluentWait_ ignores `NoSuchElement` by default and on failure would raise _TimeoutException_. Definitely OP is new and not aware about it. Possibly _FluentWait_ is success but `NoSuchElement` is comming from somewhere else. You need the 1) DOM 2) Code trials 3) Error stack trace. – undetected Selenium Aug 28 '19 at 22:19
  • 1
    @Slimy43 If you are comfortable in Selenium .For debugging this issue set System.setProperty("webdriver.chrome.verboseLogging", "true"); for Chrome driver. FluentWait wait = new FluentWait(driver).pollingEvery(Duration.ofSeconds(2)).withTimeout(Duration.ofSeconds(10)).ignoring(NoSuchElementException.class); For this wait . There should be atleast 4 - Payload with [1567051997.741][INFO]: [8e1b94fdc7f07def00e4cad2789379b9] COMMAND FindElement { "using": "xpath", in logs. Let me know what you see . – Rahul L Aug 29 '19 at 04:17
  • Also need to know Java binding version ? This will help in building new method for your code. There were some changes in ExpectedConditions https://github.com/SeleniumHQ/selenium/commit/8f7dffe2516654451fe13af5cc62656d44fc5a05 – Rahul L Aug 29 '19 at 04:30
  • 1
    Many thanks for the discussion, there's a few things there that I can try this morning. With respect to the locator, yes it is correct. As suggested in my edit, if I delay the code to the point where the DOM contains the element, everything works fine. It's interesting that you say NoSuchElement is ignored by default, you are correct that I didn't know that. I'm also very interested to see whether it's coming from somewhere else. I will rerun the test this morning with debugging set. – Slimy43 Aug 29 '19 at 06:01
  • I've updated the question with a debug log. I don't fully understand the content of it, but at the very least I do understand that the timestamps suggest it's not timing out, it's checking the presence of the element once and then throwing an exception immediately. – Slimy43 Aug 29 '19 at 08:41

0 Answers0