-1

I am scraping a website, where i need to select a value from dropdown. Then i need to click on search button. Which then populates a table with multiple pages of search results. For each result i want to click on it, which redirects to another page, extract data from that. Then come back and do the same for other search results.

select = Select(browser.find_element_by_id("dropdown id here"))
options = select.options
for index in range(0,len(options)):
    select.select_by_index(index)
    browser.find_element_by_id("checkbox").click()
    time.sleep(5)
    browser.find_element_by_id("click on search").click()
    elems = browser.find_elements_by_xpath("need to enter all href for search results")
    time.sleep(5)
    for elem in elems:
        #Need to enter each search result's href scrape within data and get back to search results again##
        elem.get_attribute("href")
        elem.click()
        
        browser.find_element_by_id("for going back to search page again").click()


 

I get this error when i'm trying to iterate

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=87.0.4280.88)

2 Answers2

1

This problem comes when the element is not able to find out as it has become stale

Try to bring the find elemenet logic inside for loop, that should solve your problem.

    elems = browser.find_elements_by_xpath((xpath)[i])
    length = elems.size();

Run your logic with number

for (int i=0;i<length; i++)
    #Need to enter each search result's href scrape within data and get back to search results again##

    elems = browser.find_elements_by_xpath("need to enter all href for search results"[i])

    elem.get_attribute("href")
    elem.click()
    time.sleep(1)
   

you can read more about it here https://stackoverflow.com/a/12967602/2986279

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
0

If you are opening new windows, you will likely want to switch focus between newly opened windows/tabs and the original.

Switch to newly opened window.

windows = browser.window_handles
browser.switch_to.window(windows[-1]) 

Close newly opened window after finding what you need on the new page, then switch back to the previous window.

browser.close()
windows = browser.window_handles
browser.switch_to.window(windows[-1]) 
Jason Cook
  • 1,236
  • 9
  • 12