我想点击下一页直到没有更多页面,但它没有点击。
返回错误:raise exception_class(message, screen, stacktrace)
StaleElementReferenceException:过时的元素引用:元素未附加到页面文档
我的代码:
提前致谢!
driver.get('http://www.chinamoney.com.cn/chinese/zjfxzx/?tbnm=%E6%9C%80%E6%96%B0&tc=null&isNewTab=1')
driver.implicitly_wait(10)
driver.refresh()
driver.implicitly_wait(10)
wait = WebDriverWait(driver, 5)
datefield_st = wait.until(EC.element_to_be_clickable((By.ID, "pdbp-date-1")))
datefield_st.click()
select_st = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-year"))))
select_st.select_by_visible_text("2021")
select2 = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-month"))))
select2.select_by_value("1")
day=1
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[@data-handler='selectDay']/a[text()='{}']".format(str(day))))).click()
datefield_ed = wait.until(EC.element_to_be_clickable((By.ID, "pdbp-date-2")))
datefield_ed.click()
select_ed = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-year"))))
select_ed.select_by_visible_text("2021")
select2 = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-month"))))
select2.select_by_value("1")
day=1
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[@data-handler='selectDay']/a[text()='{}']".format(str(day))))).click()
driver.find_element_by_link_text("查询").click()
while True:
driver.implicitly_wait(10)
links=[link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[contains(@title,'同业存单') and not(contains(@title,'申购说明')) and not(contains(@title,'公告'))]")]
titles = [title.text for title in driver.find_elements_by_xpath("//a[contains(@title,'中期票据') and not(contains(@title,'申购说明')) and not(contains(@title,'公告'))]")]
dates = [date.text for date in driver.find_elements_by_xpath('//*[@class="san-grid-r text-date"]')]
driver.implicitly_wait(10)
for link, title,date in zip(links, titles,dates):
dataframe = pd.DataFrame({'col1':date,'col2':title,'col3':link},index=[0])
dataframe.to_csv('Chinamoney.csv',mode='a+',header=False,index=False,encoding='utf-8-sig')
print(link,title,date)
try:
driver.find_element_by_xpath('//*[contains(@class, "page-next")]').click()
except:
print('No more pages')
答案 0 :(得分:1)
您将两个类名传递给选择器,而不允许按类名搜索。要么试试
(By.CLASS_NAME, 'page-next')
或
(By.CSS_SELECTOR, '.page-btn.page-next')
您的 element
和 icon
也选择相同的元素。所以你不需要定义icon
。只需使用 element.click()
答案 1 :(得分:0)
您正在使用:
driver.find_element_by_xpath('//*[contains(@class, "page-next")]').click()
试试:
element = driver.find_element_by_xpath('//*[contains(@class, "page-next")]')
driver.execute_script("arguments[0].click();", element)
如果这样不行,你可以尝试获取url/link的值并存储起来,之后你可以去这个url或者做你想做的事,而无需点击它。