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