Selenium error in Python3, element reference is stale; no longer attached to DOM, not in the context

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Selenium error in Python3, element reference is stale; no longer attached to DOM, not in the context

Saif Ayan Khan
OSX High Sierra
Selenium Version:  
3.12.0, Selenium Web Driver

I am trying to web scrap

After giving a search value, and pressing enter/return, I am getting a list of predictions, from which either I need to either double click on any of them or right click and select the fourth option from the context menu.

I am doing this for multiple searches, by passing the words in a list and entering each list item in the search box. The error is not coming for the first search result, but from the second.

The list that I have to select from I am giving as an output with indices from where the user can select, the code that does the selection is as follows - 

    for entry in inputs:
        inputElement = driver.find_element_by_id('ext-gen86')

        options = driver.find_element_by_class_name('x-grid3-scroller')
        optionText = options.text.split('\n')
        dictDis = {}
        for i,j in enumerate(optionText[1::2]):
            print (i+1, '-', j)
            dictDis[i+1] = j
        selection = input("Enter the indices you want to incorporate as patient's features separated by a space - \n").split(' ')
        for index in selection :
            text = '//*[contains(text(), "' + dictDis[int(index)] + '")]'
            toclick = driver.find_element_by_xpath(text)
            while True:
                if (toclick == driver.find_element_by_xpath(text)):
                    toclick = driver.find_element_by_xpath(text)

The above if else was just an experiment to see if it works.
I have even tried it in an infinite try except loop like

    while True:

Also I have tried giving explicit and implicit waits, but of no avail.

The same error pops up whether I use multiple different classes, or any other identification, tried path with searching for text, etc for the item to be selected; but everyone threw the same error. In the code above I am double clicking on it, but also right clicking and selecting option doesn't work.

The error is - 

    The element reference of <div id="ext-gen620" class="x-grid3-row x-grid3-row-selected "> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

P.S. - I have tried every possible solution given in a google search result upto 5 pages and also while typing the question title, I went through all the predictions that StackOverflow gave as might have my answer. In short all they say is wait for it. I did, nothing happens.

You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit
For more options, visit