Stale element error while using selenium in python

Viewed 26
import webbrowser
from selenium import webdriver
import selenium.webdriver.support.ui as uiClasses


brsr=webdriver.Chrome()
brsr.get('https://www.topuniversities.com/university-rankings/world-university-rankings/2023')
brsr.maximize_window()


brsr.find_element("xpath",'//*[@id="block-tu-d8-content"]/div/article/div/div/div[3]/div/div[1]/div/div[3]/div[4]/div[1]/div[2]/div[1]').click()

brsr.find_element("xpath",'//*[@id="block-tu-d8-content"]/div/article/div/div/div[3]/div/div[1]/div/div[3]/div[4]/div[1]/div[2]/div[2]/div[4]').click()
l_lnk=brsr.find_elements("xpath","//*[starts-with(@href,'/universities/')]")


for lnk in l_lnk:
    print(lnk.text)

I wanted to get the university names from the webpage. But it is only printing 3 or 4 university names instead of 100. and then showing,

Traceback (most recent call last):
  File "C:\Users\Shah\AppData\Local\Programs\Python\Python310\Usable\Getting names.py", line 20, in <module>
    print(lnk.text)
  File "C:\Users\Shah\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webelement.py", line 84, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "C:\Users\Shah\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webelement.py", line 396, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Shah\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Shah\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

html structure of the website

0 Answers
Related