【问题标题】:Get the numbers of pages selenium python获取硒python的页数
【发布时间】:2019-07-27 06:35:54
【问题描述】:

我有一个页面按钮列表。这是 HTML 代码。

<ul class="artdeco-pagination__pages artdeco-pagination__pages--number"><li class="artdeco-pagination__indicator artdeco-pagination__indicator--number ">
<button aria-label="page 1"> <span>1</span></button> </li>........</ul>

等等。我要做的第一件事是向下滚动页面,以查看所有页码。

driver.execute_script("window.scrollTo(0, 1500);")
pages= driver.find_elements_by_css_selector('.artdeco-pagination__indicator')
print(len(pages))

pages 结果将为 0。.artdeco-pagination__indicator 不是 CSS 选择器?我不明白为什么结果是 0 而不是 50...

【问题讨论】:

  • 有 url 或更多 html 可以分享吗?如果通过edit 共享 html 使用 sn-p 工具
  • 没有。这是某人的代码。我必须继续他的工作。
  • 所以你应该在没有 url 和源 html 的情况下工作?
  • 我更新了
      类。这就是我所拥有的。
  • css 选择器很好,但是对于实际页面,如果需要等待怎么办?大概您已经通过打开网页并输入它来查看匹配项来验证 css 选择器?如果没有网页或完整的源代码 html,我不知道您是如何测试的。你的意思是你不能分享超过上面的内容?

标签: python python-2.7 selenium selenium-webdriver web-scraping


【解决方案1】:

使用以下代码:

all_pageList = driver.find_elements_by_xpath("//li[contains(@class,'artdeco-pagination__indicator')]")
driver.find_elements_by_xpath("//li[contains(@class,'artdeco-pagination__indicator')]//button//span")[len(all_pageList)-1].text
print(len(all_pageList))

【讨论】:

    【解决方案2】:

    使用预期条件会更稳健

    pages = WebDriverWait(driver,5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.artdeco-pagination__indicator')))
    

    额外的进口

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 2018-03-22
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      相关资源
      最近更新 更多