一尘不染

尝试单击循环中的链接时出现StaleElementReferenceException

selenium

请单击下面的链接以查看我单击 ****1
的链接

BEAUTY”
。我正在使用此代码单击“ Beauty”链接

driver = webdriver.Chrome("C:\\Users\\gaurav\\Desktop\\chromedriver_win32\\chromedriver.exe")
driver.maximize_window()
driver.get("http://shop.davidjones.com.au")
object = driver.find_elements_by_name('topCategory')
  for ea in object:
  print ea.text
    if ea.text == 'Beauty':
      ea.click()
  1. 成功单击链接后,出现以下异常,有人可以告诉我为什么得到它吗?
        Traceback (most recent call last):
      File "C:/Users/gaurav/PycharmProjects/RIP_CURL/login_raw.py", line 10, in <module>  
    print ea.text  
      File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 73, in text  
    return self._execute(Command.GET_ELEMENT_TEXT)['value']  
      File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 493, in _execute  
    return self._parent.execute(command, params)  
      File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute  
    self.error_handler.check_response(response)  
      File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, 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 
      (Session info: chrome=63.0.3239.132) 
      (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.2.9200 x86_64)
    

阅读 484

收藏
2020-06-26

共1个答案

一尘不染

试试这个:

from selenium import webdriver

print("bot started")

#chromeOptions = webdriver.ChromeOptions()

#driver = webdriver.Chrome(chrome_options=chromeOptions)

def specific_text(text, ea):
    return str(text) == ea.text

driver = webdriver.Chrome("C:\\Users\\gaurav\\Desktop\\chromedriver_win32\\chromedriver.exe")
driver.maximize_window()
driver.get("http://shop.davidjones.com.au")
object_ = driver.find_elements_by_name('topCategory')
text_headers = [str(specific_text('Beauty', ea)) for ea in object_]
#print(text_headers)
index_text = text_headers.index("True")
#print(index_text)
object_[index_text].click()
2020-06-26