问题
I have the below code
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()
But i got the below error
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)
Any ideas what's wrong ?
回答1:
The DOM is rendered when you type into the password field. You need to relocate it
password = WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_class_name('dots')) # class dots is added in the rendering
password.submit()
回答2:
Sometimes it happens that when you perform any operation over an element some of it's attribute such as the class, value etc. changes and you can't access it further.
Easiest way to tackle this is identify this element again and use it, example :
pass = WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_id('pwd'))
pass.submit();
Example:
Go to the apple.com log In page, when you inspect the password field. you'll find that it's class changes from password
to password focused
when you click on the field and to password focused edited
when you type over it.
来源:https://stackoverflow.com/questions/36126385/python-selenium-selenium-common-exceptions-staleelementreferenceexception-error