【问题标题】:Issue with clicking Drop down list using Selenium in Python在 Python 中使用 Selenium 单击下拉列表的问题
【发布时间】:2013-08-05 22:35:23
【问题描述】:

我正在尝试使用 selenium 将页面保存为整个 html,页面中有一个下拉列表。该列表中有 2 个元素,我想在单击每个元素后保存整个 html。

下面是我正在使用的代码:

    devlink='http://www.t-mobile.com/content/tmo-wem/en/index/cell-phones/nokia-lumia-521.html'
    devname= devlink[devlink.rfind("/")+1:]
    ignore_tags=('script','noscript','style')
    with contextlib.closing(webdriver.Firefox()) as browser:
        browser.get(devlink) # Load page
        dropdown = browser.find_element_by_class_name('dropdown-menu')
        elements = dropdown.find_elements_by_tag_name('li')
        i=0
        for element in elements:
            element.click()
            i = i+1
            time.sleep(2)
            content=browser.page_source
            cleaner=clean.Cleaner()
            content=cleaner.clean_html(content)    
            with open(str(i)+devname,'w') as f:
               f.write(content.encode('utf-8'))

这段代码给了我一个错误,它的回溯看起来像这样Traceback (most recent call last): File "D:\Windows Phone\Pricing\August\2013-08-05\tmo test\tmo_us_selenium.py", line 52, in <module> element.click() File "C:\Python27\lib\site-packages\selenium-2.33.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 54, in click self._execute(Command.CLICK_ELEMENT) File "C:\Python27\lib\site-packages\selenium-2.33.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 228, in _execute return self._parent.execute(command, params) File "C:\Python27\lib\site-packages\selenium-2.33.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 165, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium-2.33.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 158, in check_response raise exception_class(message, screen, stacktrace) ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace: at fxdriver.preconditions.visible (file:///c:/users/abhina~1.tal/appdata/local/temp/tmpwnu8em/extensions/fxdriver@googlecode.com/components/command_processor.js:7736) at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/abhina~1.tal/appdata/local/temp/tmpwnu8em/extensions/fxdriver@googlecode.com/components/command_processor.js:10437) at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/abhina~1.tal/appdata/local/temp/tmpwnu8em/extensions/fxdriver@googlecode.com/components/command_processor.js:10456) at DelayedCommand.prototype.executeInternal_ (file:///c:/users/abhina~1.tal/appdata/local/temp/tmpwnu8em/extensions/fxdriver@googlecode.com/components/command_processor.js:10461) at DelayedCommand.prototype.execute/< (file:///c:/users/abhina~1.tal/appdata/local/temp/tmpwnu8em/extensions/fxdriver@googlecode.com/components/command_processor.js:10401)

此代码无法单击它找到的 2 个元素,请帮助我解决此问题。

【问题讨论】:

  • 您使用的是什么版本的 selenium 和 Firefox?如果粘贴完整的错误跟踪也会很有帮助 - 因为这将包含有关实际发生的错误的信息
  • Selenium-2.33.0 和 FF 22.0,我已经进行了相关更改以粘贴整个回溯

标签: python python-2.7 selenium selenium-webdriver lxml


【解决方案1】:

Selenium 2.33 支持 Firefox 21 及更早版本。您必须等待 Selenium 更新才能支持 FF 22。 有关详细信息,请参阅this 线程。 google 上有很多帖子讨论 Selenium 2.33 和 FF 22 的类似问题

【讨论】:

  • 通常情况下,FF 和 selenium Web 驱动程序兼容性问题仅在您单击或与页面元素交互时出现(例如鼠标悬停)。除了使用 Web 驱动程序单击元素之外,您在此页面上还执行了哪些其他活动?
猜你喜欢
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 2018-08-02
  • 2017-11-08
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多