使用Python中的Selenium单击下拉列表时出现问题

时间:2013-08-05 09:13:55

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

我正在尝试使用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个元素,请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

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