尝试使用Selenium Python自动化凭证代码输入

时间:2016-08-28 02:12:17

标签: python python-3.x selenium

好的,我16岁,不熟悉Python。我需要项目来帮助我学习,所以我想出了一个PlayStation Plus代码生成器作为一个项目,到目前为止它确实:

- 生成代码 - 登录索尼网站的账户管理 - 兑换部分的代码 -C-如果发生错误,则检查

voucher_box = driver.find_element_by_id("voucherCode")
redeem_button = driver.find_element_by_id("redeemGiftCardButton")
while i < 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

如果只完成ONCE,它的工作完全正常,但是如果我将数量设置为2,我收到错误消息“找到错误”然后崩溃并给我这个:

Traceback (most recent call last):
  File "C:/Users/M4SS3CR3/PycharmProjects/UKP/main.py", line 38, in <module>
    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/< (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

任何帮助或建议都将不胜感激,如果不够详细,我会事先道歉。

1 个答案:

答案 0 :(得分:1)

这是我在评论中提到的一个想法

while i < amount:
    try:
      counter = 0
      # make sure that the elements can be found
      # try ten times and then break out of loop
      while counter < 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

你也可以去做像html.find(ID)&gt;这样的事情。 -1:执行其他操作,ID不在页面上,因此退出或重新加载。还有更重要的是,移动

可能更好
voucher_box = driver.find_element_by_id("voucherCode")
          redeem_button = driver.find_element_by_id("redeemGiftCardButton")

在while循环中,因为通过everyloop对象是正确的对象。可能发生的是你正在使用属于不同dom的元素。