繁体   English   中英

使用 selenium 移动到下一页 by python

[英]moving onto the next page using selenium by python

url_main = "https://comic.naver.com/webtoon/detail.nhn?titleId=131385&no=292"

本网页最后有评论分页。 当我检查页面时,按钮的编译如下:

<a href="#" class="u_cbox_page" data-action="page#move" data-log="RPC.pgnum"><span class="u_cbox_num_page">3</span></a>

我想点击其中一个按钮,所以我试过了

driver = webdriver.Chrome(driver_path)
driver.get(url_main)
driver.switch_to.frame('commentIframe')
view_all_comments = driver.find_element_by_xpath('''//*[@id="cbox_module"]/div/div[8]/a''')
view_all_comments.click()
page_buttons = driver.find_elements_by_css_selector(".u_cbox_page")
page_buttons[2].click()

但它返回

StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=87.0.4280.88)

我怎样才能解决这个问题?

使用WebDriverWait element_to_be_clickable等待第一个按钮可点击,并使用WebDriverWait visibility_of_all_elements_located等待其他按钮的存在,如下所示:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome(driver_path)
driver.get(url_main)
driver.switch_to.frame('commentIframe')
wait = WebDriverWait(driver, 10)
view_all_comments = wait.until(ec.element_to_be_clickable((By.XPATH, '''//*[@id="cbox_module"]/div/div[8]/a''')))
view_all_comments.click()
page_buttons = wait.until(ec.visibility_of_all_elements_located((By.CSS_SELECTOR,".u_cbox_page")))
page_buttons[2].click()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM