I want to retrieve the company from a website. If without while loop, the code can run properly. But with while loop, it gives me this error: Exception has occurred: StaleElementReferenceException Message: stale element reference: stale element not found
when executed. And I can really find elements through the xpath.
My Code:
dropdown = driver.find_elements(by='xpath', value = '//div[@class="main-block flexible-submenu"]/section[@class="container border-bottom border-dark"]/div[@class=" tab-content"]/div/div/div/div[@class="dataTables_length"]/label/select/option')
i=0
while(i<len(dropdown)):
if(dropdown[i].text=="All"):
dropdown[i].click()
lists = []
stockLists = driver.find_elements(by='xpath', value = '//div[@class="main-block flexible-submenu"]/section[@class="container border-bottom border-dark"]/div[@class=" tab-content"]/div/div[@class]/div[@id="DataTables_Table_0_wrapper"]/div[@class="dataTables_scroll"]/div[@class="dataTables_scrollBody"]/table/tbody/tr/td[@class=" text-left position-relative"]')
for stockList in stockLists:
list = stockList.find_element(by="xpath", value = './a[@class="company-announcement-link"]').text
lists.append(list)
my_dict = {'stock': lists}
df_headlines=pd.DataFrame(my_dict)
df_headlines.to_csv('headline.csv')
i=i+1
driver.quit()
Error on the line:
list = stockList.find_element(by="xpath", value = './a[@class="company-announcement-link"]').text
Error Trace:
Exception has occurred: StaleElementReferenceException Message: stale element reference: stale element not found Backtrace: GetHandleVerifier [0x00A88893+48451] (No symbol) [0x00A1B8A1] (No symbol) [0x00925058]