我有以下代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.get("https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa")
username = WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_id('appleId'))
password = WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_id('pwd'))
username.send_keys("xxxxxxxxxx.com")
password.send_keys("xxxxxxxx")
password.submit()
但是我得到了以下错误
password.submit()
File "/Users/.virtualenvs/itunes/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 84, in submit
self._execute(Command.SUBMIT_ELEMENT)
File "/Users/.virtualenvs/itunes/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
return self._parent.execute(command, params)
File "/Users/.virtualenvs/itunes/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/Users/.virtualenvs/itunes/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9454)
at Utils.getElementAt (file:///var/folders/x1/1bwt313j0qvgdh5pfzpbpvcw0000gn/T/tmpR5A61x/extensions/fxdriver@googlecode.com/components/command-processor.js:9039)
at WebElement.submitElement (file:///var/folders/x1/1bwt313j0qvgdh5pfzpbpvcw0000gn/T/tmpR5A61x/extensions/fxdriver@googlecode.com/components/command-processor.js:12156)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/x1/1bwt313j0qvgdh5pfzpbpvcw0000gn/T/tmpR5A61x/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/x1/1bwt313j0qvgdh5pfzpbpvcw0000gn/T/tmpR5A61x/extensions/fxdriver@googlecode.com/components/command-processor.js:625)
任何想法有什么不对?
答案 0 :(得分:2)
键入密码字段时会呈现DOM。你需要重新安置它
password = WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_class_name('dots')) # class dots is added in the rendering
password.submit()
答案 1 :(得分:0)
有时会发生这样的情况:当您对元素执行任何操作时,某些属性(如类,值等)会发生变化,您无法进一步访问它。
解决此问题的最简单方法是再次识别此元素并使用它,例如:
pass = WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_id('pwd'))
pass.submit();
实施例:
当您检查密码字段时,转到apple.com登录页面。当您点击该字段时,您会发现它的班级从password
更改为password focused
,而当您在其上键入时,password focused edited
会更改。