简体   繁体   English

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

[英]Issue with clicking Drop down list using Selenium in Python

I am trying to save the page as entire html using selenium, page has a dropdown list in it. 我正在尝试使用硒将页面另存为整个html,页面中有一个下拉列表。 There are 2 elements in that list and I want to save the entire html after clicking each of them. 该列表中有2个元素,我想在单击每个元素后保存整个html。

Below is the code I am using: 以下是我正在使用的代码:

    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'))

This code is giving me a error, traceback of which looks like this 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) 这段代码给了我一个错误,该错误的回溯看起来像这样的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) 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)

This code is not able to click the 2 elements it is finding, please help me in resolving this issue. 此代码无法单击找到的2个元素,请帮助我解决此问题。

Selenium 2.33 supports Firefox 21 and older. Selenium 2.33支持Firefox 21及更早版本。 You will have to wait for the Selenium update to have it support FF 22. Refer to this thread for further details. 您将不得不等待Selenium更新使其支持FF22。有关更多详细信息,请参阅线程。 There are many threads on google that discuss similar issues with Selenium 2.33 and FF 22 Google上有许多主题讨论Selenium 2.33和FF 22的类似问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM