0

My java/selenium project suddenly giving errors but the elements are still there on the web application. So im getting some weird stuff suddenly. Everything was working fine till this morning. I've executed these tests like 1000 times before and there hasn't been changed anything to the front-end in the meanwhile.

Examples of errors:

org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}

or

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.linkText: Betalingsregeling (tried for 10 second(s) with 500 milliseconds interval)

or

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=77.0.3865.90)

or

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input[id*='searchCriteria[0]'][class*='col-sm-8']"} (Session info: chrome=77.0.3865.90)

3 Answers3

0

You just need to use either explicit or implicit wait. Element is available on Page but not accessible by Selenium so you have to wait till it is available to access.

Pratik
  • 357
  • 1
  • 9
  • I have wait everywhere in my code. It was working fine the last 1000x – Yalcin Batur Sep 30 '19 at 14:28
  • Use Thread.sleep("3000") and if the problem is of the wait you will know. Basically, such errors are because of the wait only. – Pratik Sep 30 '19 at 14:30
  • I doubt waiting will resolve it. I ran into the same error: "Cannot find context with specified id". To find any element, the code we built loops every 100ms to try to get or click an element for as long as 60 seconds. So that is 600x retry at max. The page was just loaded fine. But this error was shown. – Mike de Klerk Oct 02 '19 at 04:43
  • @MikedeKlerk he is saying his all his elements are there on the page so I think only reason of not locating the webelement is driver not waiting for it. Or can change xpath or css with other attribute name and value. – Pratik Oct 02 '19 at 19:47
  • We dealt with a page that has frames. And to be able to find elements we had to switch to one of the frames. At some time during the test (not always) the driver said it was unable to find an element. Upon inspection it turned out that it no longer was within the context of that frame. Eventhough there is no code that navigates away from the frame. Now, when an error like that occurrs, we made the code switch to the frame again (if possible) and try again. It seemed to have resolved our issues. – Mike de Klerk Oct 03 '19 at 05:57
  • ok im going to try that, indeed ive read some other topics it could be because of the frames. The application im testing is overloaded with frames :( – Yalcin Batur Oct 03 '19 at 08:40
  • well i've exectued while debugging the places where it fails normally... its working all fine... the only thing i can think of now is that the pages of application are loading to fast and the explicit wait.expectedconditions is failing because the element is found but the DOM is not really loaded. is that a possibility? What can i do about this? – Yalcin Batur Oct 03 '19 at 10:25
  • @YalcinBatur in that case you can either use thread.sleep() or try changing the xpath or css. – Pratik Oct 03 '19 at 13:07
  • ive changed all my waits in the code from Excpectedondition.presenceOfElement to elementToBeClickable and still giving same errors – Yalcin Batur Oct 03 '19 at 13:49
  • Use thread.sleep(5000) to test whether its a problem of the wait or something else! Once you find out its about wait then you can proceed in changing wait. – Pratik Oct 03 '19 at 14:18
0

Sounds like your website changed. Check the IDs with F12 in chrome on the website you normally test and make sure they're all still the same.

Alternatively; you could be experiencing the typical "My application was running all weekend and now it's slow on Monday morning" latency. If this is the case, @Pratik's answer is correct, but try increasing the Timeout instead of adding hard-coded Sleeps.

My only other guess, based on about half of those errors, is that you're accidentally referencing an older WebDriver, and not using the one that you think you're using.

Erin B
  • 103
  • 2
  • 10
0

I have fixed the issue with some Thread.sleep after switching to iframes. Dont know why the explicit wait after switching to iframes and before finding the element fails, i have the timeout set on 15 sec and built explicit waits before every element. Apparently the elements inside the frames suddenly didnt have enough time to fully load.....

 public static void switchToIFramesOfSearchPage() {
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(SEARCH_PAGE_IFRAME1_ID_LOCATOR_VALUE);
    driver.switchTo().frame(SEARCH_PAGE_IFRAME2_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfToolbar() {
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(TOOLBAR_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIframeOfActivitiesPane(){
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(ACTIVITIES_PANE_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(750);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfHistoryPane() {
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(HISTORY_PANE_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfImportantView(){
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(IMPORTANT_VIEW_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfAccountView(){
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(ACCOUNT_VIEW_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}