我已經用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> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl02','')">2</a> <span>3</span> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl04','')">4</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl05','')">5</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl06','')">6</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl07','')">7</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl08','')">8</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl09','')">9</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl10','')">10</a> <a href="javascript:__doPostBack('dgAwards$ctl01$ctl11','')">...</a></td>
</tr>
順便說一句,分頁選項出現在點擊中的 「搜索」 按鈕主頁。
'wait.until'不返回元素。你需要使用'find_element(s)'方法 – Amit
我首先嚐試了這個方法,但是這也沒有帶來任何幫助。 – SIM
你在哪裏得到異常 – Amit