【问题标题】:selenium with python web crawlerselenium 与 python 网络爬虫
【发布时间】:2013-01-09 01:47:59
【问题描述】:

我想筛选一个有多个页面的网站。这些页面在不更改 URL 的情况下动态加载。因此我使用硒来筛选它。但是这个简单的程序我遇到了一个例外。

import re
from contextlib import closing
from selenium.webdriver import Firefox 

url="http://www.samsung.com/in/consumer/mobile-phone/mobile-phone/smartphone/"

with closing(Firefox()) as browser:
    n = 2
    link = browser.find_element_by_link_text(str(n))
    link.click()
    #web_page=browser.page_source
    #print type(web_page)

错误如下

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"2"}' ; Stacktrace: Method FirefoxDriver.prototype.findElementInternal_ threw an error in file:///tmp/tmpMJeeTr/extensions/fxdriver@googlecode.com/components/driver_component.js 

是给定的url还是firefox浏览器的问题。 如果有人帮助我,那将是很大的帮助。

【问题讨论】:

    标签: python selenium web-crawler


    【解决方案1】:

    我认为您的主要问题是页面本身需要一段时间才能加载,并且您立即尝试访问该链接(可能尚未呈现,因此堆栈跟踪)。您可以尝试的一件事是使用隐式等待1 和您的browser,这将告诉browser 在超时之前等待一段时间以使元素出现。在您的情况下,您可以尝试以下操作,这将在轮询 DOM 以获取特定项目(在本例中为链接文本 2)时最多等待 10 秒:

    browser.implicitly_wait(10)
    n = 2
    link = browser.find_element_by_link_text(str(n))
    link.click()
    #web_page=browser.page_source
    #print type(web_page)
    

    【讨论】:

      【解决方案2】:

      我正在开发一个可能涵盖您(或其他人)用例的 python 模块:

      https://github.com/cmwslw/selenium-crawler

      它将记录的 selenium 脚本转换为爬虫函数,从而避免编写任何上述代码。它适用于动态加载内容的页面。我希望有人觉得这很有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多