我试图计算从this site单击“加载更多评论”选项的次数。但是我收到以下错误:
selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档
这是我的python代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
url = "https://www.justdial.com/Delhi/S-K-Premium-Par-Hari-Nagar/011PXX11-XX11-131128122154-B8G6_BZDET"
driver.get(url)
pop_up = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="best_deal_detail_div"]/section/span')))
pop_up.click() # For disable pop-up
count = 0
while True:
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//span[text()='Load More Reviews..']")))
element.click()
count = count + 1
print(count)
答案 0 :(得分:1)
尝试以下代码:
count = 0
while True:
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Load More Reviews..']"))).click()
count = count + 1
except StaleElementReferenceException:
pass
except TimeoutException:
break
print(count)
问题:根据您的代码,您正在等待加载更多评论按钮,现在单击该按钮一次,甚至在页面完成加载其检测按钮之前在那里并且可以点击,但是当尝试点击时,该页面仍在刷新/加载更多评论。结果HTML DOM
被破坏/刷新,stale element exception
出现了。
由于您的代码中没有 break
条件,因此我添加了一个条件。如果页面上没有“加载更多评论”按钮。它将跳出循环。