title: Selenium & Python (looping through a password list)
body: <p>I have a list of strings that I am looking to enter in a password field using selenium and Python 2.7x. Below is my code:</p>
  
  <pre><code># album is a URL
  def password_attempt(album):
      fp = webdriver.FirefoxProfile()
      fp.set_preference("network.proxy.type", 1)
      fp.set_preference("network.proxy.socks", "127.0.0.1")
      fp.set_preference("network.proxy.socks_port", 9150)
      fp.update_preferences()
      driver = webdriver.Firefox(firefox_profile=fp)
      driver.get(album)
      elem = driver.find_element_by_name("pwd")
      for password in password_list:
          elem.send_keys(password)
          elem.send_keys(Keys.RETURN)
          if album in driver.current_url:
              continue
          else:
              return driver.current_url
  </code></pre>
  
  <p>This code doesn't run and I get an error returned when it's run. In essence, when a password from the list is entered, if the password is incorrect, the page simply reloads with the same URL as the stem and a few random characters at the end of the URL. I'm then looking to move on to the next password in the list and try that. If the password is indeed accepted, the page then redirects to a new page with a new URL that doesn't contain any content from the original URL.</p>
  
  <p>So the original URL may be <code>http://www.myalbum.com</code> and if an incorrect password is entered, it redirects to <code>http://www.myalbum.com/id=12344</code>. If a correct password is entered however, it redirects to <code>http://www.abc124.com</code></p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Dell\Desktop\code.py", line 203, in &lt;module&gt;
      thumbnail_identifier()
    File "C:\Users\Dell\Desktop\code.py", line 134, in thumbnail_identifier
      new_album = password_attempt(album)
    File "C:\Users\Dell\Desktop\code.py", line 186, in password_attempt
      elem.send_keys(Keys.RETURN)
    File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 303, in send_keys
      self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
    File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 385, in _execute
      return self._parent.execute(command, params)
    File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
      self.error_handler.check_response(response)
    File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
      raise exception_class(message, screen, stacktrace)
  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:8329:1)
      at Utils.getElementAt (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:7922:10)
      at fxdriver.preconditions.visible (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:8957:11)
      at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11618:15)
      at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:11)
      at DelayedCommand.prototype.executeInternal_ (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11640:7)
      at DelayedCommand.prototype.execute/&lt; (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11582:5)
  </code></pre>
  
  <p>Any help?</p>
  
  <p><strong>UPDATE:</strong> I restructed my code as below:</p>
  
  <pre><code>for password in password_list:
      elem = driver.find_element_by_name("pwd")
      elem.send_keys(password)
      elem.send_keys(Keys.RETURN)
      if album in driver.current_url:
          continue
      else:
          return driver.current_url
  </code></pre>
  
  <p>A segment from the error is below:</p>
  
  <pre><code>StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
  Stacktrace:
  </code></pre>