无法继续下一页

2024-10-03 15:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写了一个脚本,使用selenium遍历从第一页开始的不同页面,直到分页。但是,除了一些数字外,下一页按钮没有选项。当我点击那个号码,它会把我带到下一页。不管怎样,当我试着用我的脚本做这件事时,它确实点击了第二页并转到那里,但是它不再滑动了,我的意思是它没有继续到第三页,而是抛出以下错误而中断。在

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

我正在尝试的脚本:

^{pr2}$

其中分页编号为:

<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>

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


Tags: 脚本选项seleniumelementjavascript按钮trtd
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:33

您不能遍历预定义元素的列表,因为在您使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

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

相关问题 更多 >