Python Forum

Full Version: Selenium stale element reference
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello again!
So, i am able to run selenium again, now that i have recovered from the "fatal error"
The page i am visiting has multiple links with the link text as 'View'. Behind each of these is a pdf file, which i am now able to download on clicking (since i downloaded chromedriver after facing this problem).

I understand that any activity on the page may make the references generated before it stale. For that, i have written the following:
	viewsList = reversed(browser.find_elements_by_link_text('View'))
	for link in viewsList:
		window_before = browser.window_handles[0]
		link.click()
		time.sleep(2)
		browser.switch_to_window(window_before)
		viewsList = reversed(browser.find_elements_by_link_text('View'))
What i need the program to do is to sequentially download each pdf file.
Can someone please tell me where i am going wrong and what i can do to make this work?
Thank you!
I got it to work using xpath instead of link text. As my links were in one column in a table, i just had to change the row number in the xpath for each iteration. As xpath will remain constant even if the page is reloaded (this i am concluding because..), this code works.

	views = browser.find_elements_by_link_text('View')
	a = len(views) + 1
	views = browser.find_element_by_xpath('//form/div[3]/center/div[3]/table/tbody/tr[4]/td/div[1]/table/tbody/tr[' + str(a) + ']/td[4]/a')	
	while a > 1: #to exclude the header row
		window_before = browser.window_handles[0]
		views.click()
		browser.switch_to_window(window_before)
		a -= 1
		if a != 1:
			views = browser.find_element_by_xpath('//form/div[3]/center/div[3]/table/tbody/tr[4]/td/div[1]/table/tbody/tr[' + str(a) + ']/td[4]/a')
:)