2

I'm trying to download all the pdf on a webpage using Selenium Python with Chrome as browser but every time the session ends with this message:

StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.22.397933

This is the code:

def download_pdf(self):
    current = self.driver.current_url        
    lista_link_temp = self.driver.find_elements_by_xpath("//*[@href]")
    for link in lista_link_temp:
        if "pdf+html" in str(link.get_attribute("href")):
            tutor = link.get_attribute("href")
            self.driver.get(str(tutor))
            self.driver.get(current)

Please help me.. I've just tried lambda, implicit and explicit wait

Thanks

2 Answers 2

1

As soon as you call self.driver.get() in your loop, all the other elements in the list of elements will become stale. Try collecting the href attributes from the elements first, and then visiting them:

def download_pdf(self):
    current = self.driver.current_url
    lista_link_temp = self.driver.find_elements_by_xpath("//*[@href]")
    pdf_hrefs = []

    # You could do this part with a single line list comprehension too, but would be really long...
    for link in lista_link_temp:
        href = str(link.get_attribute("href"))
        if "pdf+html" in href:
            pdf_hrefs.append(href)
    for h in pdf_hrefs:
        self.driver.get(h)
        self.driver.get(current)
Sign up to request clarification or add additional context in comments.

1 Comment

@LBdoc Glad to hear it!
1

You get stale element when you search for an element and before doing any action on it the page has changed/reloaded.

Make sure the page is fully loaded before doing any actions in the page.

So you need to add first a condition to wait for the page to be loaded an maybe check all requests are done.

2 Comments

Thanks..I tried to use implicit and explicit wait without success..I tried even with time.sleep in order to wait for page load..
You are right that the elements are stale because they have been reloaded, but as far as I know it is not a matter of waiting for the items to load; once the go stale you must find_* them again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.