【问题标题】:Can't move on to the next page无法进入下一页
【发布时间】:2018-01-22 15:14:46
【问题描述】:

我用 selenium 在 python 中编写了一个脚本来遍历从第一页到分页的不同页面。但是,除了一些数字外,没有下一页按钮的选项。当我单击该数字时,它会将我带到下一页。无论如何,当我尝试使用我的脚本执行此操作时,它确实会单击第二页并转到那里,但它不再滑动,我的意思是它不会继续进入第三页,而是抛出以下错误。

line 192, 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

我正在尝试的脚本:

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

driver = webdriver.Chrome()
driver.get("http://www.cptu.gov.bd/AwardNotices.aspx")
wait = WebDriverWait(driver, 10)
driver.find_element_by_id("imgbtnSearch").click()
for item in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#dgAwards > tbody > tr > td > a"))):
    item.click()
driver.quit()

分页编号所在的元素:

<tr align="right" valign="top" style="font-size:XX-Small;font-weight:normal;white-space:nowrap;">
        <td colspan="8"><span>Page: </span><a href="javascript:__doPostBack('dgAwards$ctl01$ctl01','')">1</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl02','')">2</a>&nbsp;<span>3</span>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl04','')">4</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl05','')">5</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl06','')">6</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl07','')">7</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl08','')">8</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl09','')">9</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl10','')">10</a>&nbsp;<a href="javascript:__doPostBack('dgAwards$ctl01$ctl11','')">...</a></td>
    </tr>

顺便说一句,点击主页上的“搜索”按钮会出现分页选项。

【问题讨论】:

  • wait.until 不返回元素。你需要使用find_element(s)方法
  • 我一开始就尝试过,但也没有带来任何帮助。
  • 你在哪里得到异常
  • 在第 192 行。就在“item.click()”之后
  • @Amit,错了! wait.until(EC.presence_of_..._located((selector))) 返回一个元素或元素列表取决于确切的 EC 方法

标签: python python-3.x selenium selenium-webdriver web-scraping


【解决方案1】:

您无法遍历预定义元素的列表,因为在您刷新 click() 页面后,这些元素会变得陈旧

你可以试试下面的:

from selenium.common.exceptions import NoSuchElementException    

page_counter = 2
while True:
    try:
        if not page_counter % 10 == 1:
            driver.find_element_by_link_text(str(page_counter)).click()
            page_counter += 1
        else:
           driver.find_elements_by_link_text("...")[-1].click() 
           page_counter += 1
    except NoSuchElementException :
        break

这应该允许您在可能的情况下切换到下一页

【讨论】:

  • 安德森先生,您是巫师。这是我遇到过的最好的技术。
  • 有一件事先生-为什么百分比“page_counter % 10”?原谅我的无知。
  • 在此内容中,它不是百分比,而是modulopage_counter % 10 == 1 表示返回 page_counter 等于 10 + 1 (11)、20 + 1 (21)、30 + 1 (31)... 这是因为不是带有文本的链接 @987654333 @,21,31...有链接...
  • 你成就了我的一天,先生。谢谢一万亿。
  • 安德森先生请看这里,“stackoverflow.com/questions/45754132/…
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
相关资源
最近更新 更多