无法转到下一页

时间:2017-08-14 17:59:57

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

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

我正在尝试的脚本:

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>

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

1 个答案:

答案 0 :(得分:3)

您无法遍历预定义元素列表,因为在您进行<?php class BancoDados { var $host; var $usuario; var $senha; var $nomeBancoDados; var $prefixoTabelas; var $conexao; function __construct($hostBD, $usuarioBD, $senhaBD, $nomeBD, $prefixoTabelasBD) { $this->host = $hostBD; $this->usuario = $usuarioBD; $this->senha = $senhaBD; $this->nomeBancoDados = $nomeBD; $this->prefixoTabelas = $prefixoTabelasBD; } function voltaValor() { return "Dados: " . $host . " " . $nomeBancoDados . " " . $prefixoTabelas . " " . $usuario . " " . $senha; } function conectar() { $retorno = true; $this->conexao = mysqli_connect($host, $usuario, $senha); if(!$this->conexao) { $retorno = false; } return $retorno; } function desconectar() { mysqli_close($this->conexao); } } ?> 页面刷新并且这些元素变得陈旧之后

您可以尝试以下内容:

click()

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