尝试使用seleniumpython自动输入凭证代码

2024-04-20 01:14:27 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我今年16岁,对Python还是个新手。我需要做一些项目来帮助我学习,所以我提出了一个PlayStation Plus代码生成器作为一个项目,到目前为止它做到了:

-生成代码 -登录索尼网站的帐户管理 -在兑换区输入代码 -检查是否发生错误

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

如果只执行一次,它就可以完全正常工作,但是如果例如,我将amount设置为2,我会收到错误消息“error found”,然后它崩溃并给出以下信息:

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)

任何帮助或建议将不胜感激,我在此之前道歉,如果这是不够详细。你知道吗


Tags: incomlocaljsextensionsusersappdatatemp
1条回答
网友
1楼 · 发布于 2024-04-20 01:14:27

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

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)>;-1:执行其他操作该ID不在页面上,因此退出或重新加载。更重要的是,搬家可能更好

voucher_box = driver.find_element_by_id("voucherCode")
          redeem_button = driver.find_element_by_id("redeemGiftCardButton")

在while循环中,因为这样通过everyloop,对象就是正确的对象。可能发生的情况是,您正在使用属于不同dom的元素。你知道吗

相关问题 更多 >