1

Scraping next pages issue with selenium

is somewhat similar althogh i dont want to make it more complicated by using with bs4

I want to scrape all 10 titles (links to pdfs) on every page. Here is the code that throws a StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (at i.click())

driver.find_element_by_css_selector('button[class="btn btn-primary"][type="submit"]').click()    
    
for j in range(10):
    pdfs = driver.find_elements_by_css_selector(".title a")
    if j < 10:
        nextpg = driver.find_element_by_css_selector(".tx-pagebrowse-page button")
        pdfs = driver.find_elements_by_css_selector(".title a")
    for i in pdfs:
        i.click()
        time.sleep(5)
    if j < 10:
        nextpg.click()
    else:
        break

below it shows that the first page is a type=list list with the right titles

pdfs = driver.find_elements_by_css_selector(".title a")
print(type(pdfs))
for my_pdf in pdfs:
    print(my_pdf.get_attribute('innerHTML'))

<class 'list'>
                                dfhgadr asfdgsd
                                sDF SDF 
                                SDFSDF sdf
                                SDF DSfdf
                                SDFD sdf
                                SDFSdf fdf
                                zewr4
                                sHSDFG 
                                RSZJRT
                                dhff fgf
id345678
  • 97
  • 1
  • 3
  • 21

1 Answers1

0

Instead of pdfs[[i]].click() You may try as follows:

 i.click()
Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32