Selenium clicks one time, but next click returns StaleElementReferenceException

前端 未结 1 1047
无人及你
无人及你 2020-12-21 23:59
import sys
import urllib2
import time
from bs4 import BeautifulSoup
from selenium import webdriver
import string
import re

reload(sys)
sys.setdefaultencoding(\'utf8         


        
相关标签:
1条回答
  • 2020-12-22 00:16

    The error says it all :

    selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
    

    In your program within the for() loop you are locating the <a> tag element with text as >> on a page and invoking click() and due to the click() event the HTML DOM changes. When your program iterates the for() loop for the second time perhaps the WebElement identified as driver.find_element_by_xpath("//a[contains(text(),'>>')]") doesn't gets loaded but Selenium tries to refer the search from the previous iteration which have already turned stale. Hence you see StaleElementReferenceException.

    Solution

    A convincing way to iterate the pages would be instead of :

    driver.find_element_by_xpath("//a[contains(text(),'>>')]").click()
    

    You can induce WebDriverWait in-conjunction with expected_conditions clause set to element_to_be_clickable for the WebElement with the particular Page Number (e.g. 3, 4, 5, etc) to be clickable as follows :

    WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'3')]"))).click()
    
    0 讨论(0)
提交回复
热议问题