Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium stale element reference
#1
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!
Reply
#2
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')
:)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium suddenly fails to find element Pavel_47 3 6,207 Sep-04-2022, 11:06 AM
Last Post: Pavel_47
  Clicking on element not triggering event in Selenium Python (Event Key is not in data dkaeloredo 2 4,236 Feb-16-2020, 05:50 AM
Last Post: dkaeloredo
  stale element reference keuninkske 1 2,285 Jan-19-2020, 09:56 AM
Last Post: keuninkske
  Selenium locating an element. JokerTux 3 2,640 Dec-28-2019, 08:50 AM
Last Post: snippsat
  Selenium returning web element instead of desired text newbie_programmer 1 5,145 Dec-11-2019, 06:37 AM
Last Post: Malt
  Python Selenium getting table element trengan 2 8,505 Dec-31-2018, 03:02 PM
Last Post: trengan
  Click Element if displayed using Selenium and Python giaco__mar 1 3,492 Dec-27-2018, 06:19 PM
Last Post: metulburr
  Simple Element Check Code in Selenium Not Working digitalmatic7 1 2,987 Feb-18-2018, 06:53 AM
Last Post: metulburr
  Selenium - Googlemaps element not visible Barnettch3 3 5,576 Jan-15-2018, 08:07 PM
Last Post: Barnettch3
  locating element with selenium after login sumandas89 1 3,608 Jan-15-2018, 02:40 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020