本文介绍了python - 如何单击“下一步"在 Selenium 中直到它不再可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面上抓取一些数据并将每个页面中的表格提取到数据框中,然后单击下一步"按钮并在第二页上抓取一些数据,直到它不再可用.我怎样才能用这个 html 结构制作它:

<ul><li class="active"><a href="javascript:void(0);">首先</a></li><li class="active"><a href="javascript:void(0);">上一页</a></li><li p="1" class="active" onclick="loadDataByPage(1);"><a href="javascript:void(0);"style="color:#0D88CB">1</a><li p="2" class="inactive" onclick="loadDataByPage(2);"><a href="javascript:void(0);">2</a><li p="3" class="inactive" onclick="loadDataByPage(3);"><a href="javascript:void(0);">3</a><li p="2" class="inactive" onclick="loadDataByPage(2);"><a href="javascript:void(0);"style="color:#0D88CB">下一步</a><li onclick="loadDataByPage(490);"类=非活动"><a href="javascript:void(0);"style="color:#0D88CB">最后</a>

解决方案

你应该使用 looping 如下:-

from selenium.webdriver.common.by import By从 selenium.webdriver.support.ui 导入 WebDriverWait从 selenium.webdriver.support 导入 expected_conditions 作为 EC等待 = WebDriverWait(驱动程序,10)为真:# 在页面上抓取一些数据并提取表# 点击下一个链接尝试:element = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), 'Next')]")))element.click()除了超时异常:休息

希望能帮到你..:)

I would like to scrape some data on a page and extract table from each page to a dataframe, then click on the "next" button and scrape some data on the second page until it's no longer available. How can I make it with this html structure:

<div class="dataTables_paginate paging_bootstrap pagination">
 <ul>
  <li class="active"><a href="javascript:void(0);">First</a></li>
  <li class="active"><a href="javascript:void(0);">Prev</a></li>
  <li p="1" class="active" onclick="loadDataByPage(1);">
   <a href="javascript:void(0);" style="color:#0D88CB">1</a>
  </li>
  <li p="2" class="inactive" onclick="loadDataByPage(2);">
   <a href="javascript:void(0);">2</a>
  </li>
  <li p="3" class="inactive" onclick="loadDataByPage(3);">
   <a href="javascript:void(0);">3</a>
  </li>
  <li p="2" class="inactive" onclick="loadDataByPage(2);">
   <a href="javascript:void(0);" style="color:#0D88CB">Next</a>
  </li>
  <li onclick="loadDataByPage(490);" class="inactive">
   <a href="javascript:void(0);" style="color:#0D88CB">Last</a>
  </li>                
 </ul>
</div>
解决方案

You should use looping as below :-

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)

while True:
# scrape some data on a page and extract table

# click next link
try:
    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), 'Next')]")))
    element.click()
except TimeoutException:
    break

Hope it will help you..:)

这篇关于python - 如何单击“下一步"在 Selenium 中直到它不再可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 17:38