【问题标题】:How do I get to the next web page on this site?如何进入本网站的下一个网页?
【发布时间】:2018-04-08 08:35:54
【问题描述】:

我正在尝试访问 JavaScript 动态站点的下一页。 http://msds.walmartstores.com/ 我一直试图激活以进入下一页的元素是:

<a href="#" class="next" data-action="next">›</a> 

在这个网站上: http://msds.walmartstores.com/

可以在其中找到:

<div class="pagination" id="pagination" style="">
    <a href="#" class="first" data-action="first">«</a>
    <a href="#" class="previous" data-action="previous">‹</a>
    <input type="text">
    <a href="#" class="next" data-action="next">›</a>
    <a href="#" class="last" data-action="last">»</a>
</div>

我能够获取我想要在此页面上抓取的所有 JavaScript 元素(PDF)。如何进入下一页?

我遇到的问题/尝试:

1

代码:

driver.find_element_by_class_name("next").click()

错误:

Traceback (most recent call last):
  File "<stdin>", line 56, in <module>
  File "<stdin>", line 50, in main
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 77, in click
self._execute(Command.CLICK_ELEMENT)
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 494, in _execute
return self._parent.execute(command, params)
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 236, in execute
self.error_handler.check_response(response)
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element i
s not clickable at point (251, 2173)
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cf
d9),platform=Windows NT 6.1.7601 SP1 x86_64)

shell returned 1
Hit any key to close this window...

2

代码:

driver.find_element_by_class_name("next").submit()

错误:

Traceback (most recent call last):
  File "<stdin>", line 56, in <module>
  File "<stdin>", line 50, in main
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 88, in submit
    self._execute(Command.SUBMIT_ELEMENT)
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 494, in _execute return self._parent.execute(command, params)
  File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", 
line 236, in execute self.error_handler.check_response(response)
  File "E:\Python27\lib\sitepackages\selenium\webdriver\remote\errorhandler.py", line 192, 
in check_response raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Element was not in a form, so could not submit.
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),
platform=Windows NT 6.1.7601 SP1 x86_64)

shell returned 1
Hit any key to close this window...

这是我打开网页的代码:

import time
from selenium import webdriver
import os

url = "http://msds.walmartstores.com/"
    myfile = open("PDFLinks.txt", "w")
    driver = webdriver.Chrome()
    driver.get(url)

感谢您的帮助

【问题讨论】:

    标签: python python-2.7 selenium web-scraping


    【解决方案1】:

    我想出的解决方案:

    由于我尝试过的所有操作都给了我错误,并且尝试单击该元素给了我一个“未知错误:元素在点 (251, 2173) 处不可点击”,我想出了一个解决方法。

    在网站的底部有一个地方可以让我插入一个页面的数字,我用它来移动到下一页并在我的程序中有一个计数器。

    import time
    from selenium import webdriver
    import os
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    
    
    while is_next_there(driver):
    
        go_through_page(driver, myfile, page)
    
        #Gets the next page, and waits for load
        el = wait.until(EC.presence_of_element_located((By.XPATH,"//div[@class='pagination' and @id='pagination']")))
        el.find_element_by_xpath(".//input").clear()
        el.find_element_by_xpath(".//input").send_keys(page)
        el.find_element_by_xpath(".//input").send_keys(Keys.ENTER)
        page += 1
    

    通过这个解决方案,我能够成功进入下一页

    【讨论】:

      【解决方案2】:

      尝试使用 xpath:

      el= driver.find_element_by_xpath("//div[@class='pagination' and @id='pagination']")
      el.find_element_by_xpath(".//a[@class='next']").click()
      

      编辑

      我试过这个 html(基于你的):

      <!DOCTYPE html>
      <html>
      <body>
      <script>
      function myFunction() {
          alert("Hello! I am an alert box!");
      }
      </script>
      
      <div class="pagination" id="pagination" style="">
          <a href="#" class="first" data-action="first">«</a>
          <a href="#" class="previous" data-action="previous">‹</a>
          <input type="text">
          <a href="#" class="next" data-action="next" onclick="myFunction()">›</a>
          <a href="#" class="last" data-action="last">»</a>
      </div>
      
      </body>
      </html>
      

      它有效。可能你需要做更多关于你的 html 的信息。

      EDIT2

      我查看了您感兴趣的网页。从您的代码中不清楚您要做什么,并且在您的问题中没有说明要执行的步骤。

      我注意到,如果我单击“提交”按钮,将会出现一个 .pdf 列表。在这种情况下,存在您感兴趣的元素。 所以,我尝试了这个:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.support.wait import WebDriverWait
      
      driver.get("http://msds.walmartstores.com/")
      driver.find_element_by_id("submitUPC").click()
      wait = WebDriverWait(driver, 20)
      el= wait.until(EC.presence_of_element_located((By.XPATH,"//div[@class='pagination' and @id='pagination']")))
      el.find_element_by_xpath(".//a[@class='next']").click()
      

      【讨论】:

      • 感谢您的回答,但不幸的是它没有解决我的问题:(
      • 我刚刚发布了问题中网站的链接,msds.walmartstores.com
      • 我的 selenium 包有问题吗?明天我得再研究一下
      【解决方案3】:

      我使用 C#,但这应该很接近。对于难以触及的元素,我对 CSS 选择器有更多的运气。 我没有看到 iframe 或任何奇怪的东西,所以这应该可以。这使用 CSS 选择器和 JavaScript 的组合来定位并单击您的下一步按钮。您可能需要从 C# 到 Python 进行一些调整,但这应该可以解决您的问题。

      By next = By.CssSelector("#pagination > a.next");
              IWebElement nextButton = driver.FindElement(next);
      
      IJavaScriptExecutor clickNextButton = driver as IJavaScriptExecutor;
              clickNextButton.ExecuteScript("arguments[0].click();", nextButton);
      

      【讨论】:

      • 仅供参考,我知道这可以缩短,但我认为这是一个更好的例子,因为它显示了所有步骤。
      猜你喜欢
      • 2021-12-30
      • 2021-09-07
      • 2021-09-03
      • 2019-12-22
      • 2012-12-16
      • 2021-06-03
      • 1970-01-01
      • 2015-01-05
      相关资源
      最近更新 更多