Trying to automate voucher code input with Selenium Python
<p>Ok so, I'm 16 and am new to Python. I need projects to do to help me learn, so I came up with a PlayStation Plus code generator as a project and so far it does:</p>

<p>-Generates a code
-Logs in to Account Management at Sony's site
-Enters code at redeem section
-Checks if error occurred</p>

<pre><code>voucher_box = driver.find_element_by_id("voucherCode")
redeem_button = driver.find_element_by_id("redeemGiftCardButton")
while i &lt; amount:
    voucher_box.clear()
    currentcode = codegen.codegen()
    voucher_box.send_keys(currentcode)
    redeem_button.click()
    if "The Prepaid Card code you entered is incorrect or is no longer valid" in driver.page_source:
        print("Error found")
    else:
        print("Error not found")
    i += 1
</code></pre>

<p>It works completely fine if only done ONCE but if for example, I set amount to 2, I get my error message "Error found" and then it crashes and gives me this:</p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/M4SS3CR3/PycharmProjects/UKP/main.py", line 38, in &lt;module&gt;
    voucher_box.clear()
  File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 87, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, 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:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:9039)
    at fxdriver.preconditions.visible (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:10090)
    at DelayedCommand.prototype.checkPreconditions_ (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/&lt; (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
</code></pre>

<p>Any help or advice would be appreciated, I apologize before hand if this is not detailed enough.</p>

<p>This is an idea of what I mentioned in the comment</p>

<pre><code>while i &lt; amount:
    try:
      counter = 0
      # make sure that the elements can be found
      # try ten times and then break out of loop
      while counter &lt; 10:
        try:
          voucher_box = driver.find_element_by_id("voucherCode")
          redeem_button = driver.find_element_by_id("redeemGiftCardButton")
          break
        except:
          time.sleep(1)
          counter += 1

      voucher_box.clear()
      currentcode = codegen.codegen()
      voucher_box.send_keys(currentcode)
      redeem_button.click()
      if "The Prepaid Card code you entered is incorrect or is no longer valid" in driver.page_source:
          print("Error found")
      else:
          print("Error not found")
      i += 1
      driver.get("my page") # may not need this line because the elements were moved inside the while loop
      time.sleep(3) # give the page enough time to load
    except:
      pass
</code></pre>

<p>You can also go as far as to do something like html.find(ID) > -1: do something else the ID wasn't present on the page so quit or reload. Also more then anything else it's probably better to move </p>

<pre><code>voucher_box = driver.find_element_by_id("voucherCode")
          redeem_button = driver.find_element_by_id("redeemGiftCardButton")
</code></pre>

<p>inside the while loop because so that the object is the correct object through everyloop.  What could be happening is that you're using elements that belonged to a different dom.</p>

python python-3.x selenium