【问题标题】:Trying to automate voucher code input with Selenium Python尝试使用 Selenium Python 自动输入凭证代码
【发布时间】:2016-08-28 02:12:16
【问题描述】:

好吧,我今年 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

如果只执行一次,它完全可以正常工作,但是例如,如果我将数量设置为 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)

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

【问题讨论】:

  • Selenium 很棘手...话虽如此,它似乎正在崩溃,因为在页面上找不到该元素。您可能希望在单击按钮后添加 time.sleep(3)给页面足够的时间来重新加载元素...还要确保在您提交一次后页面确实包含 ID 凭证代码。对此进行测试的一个简单原因是使用 chrome 工具并手动执行该过程以检查页面中的更改 html
  • @reticentroot 我已经尝试了你所说的两种方法。 time.sleep() 没有帮助,再次出现同样的错误。提交后我还检查了元素,它仍然存在,相同的 ID 和所有内容。
  • 在你 i+=1 之后如何强制 selenium 重新加载页面页面 (driver.get()) 并在 while 循环中移动你的 voucher_box 变量和redeem_button 变量。这样每次都能找到元素。最终,您可以将 while 循环中的所有内容包装在 try except 块中,并在异常通过或睡眠时进行。

标签: python python-3.x selenium


【解决方案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) > -1 之类的操作:执行其他 ID 不在页面上的操作,因此退出或重新加载。也比其他任何事情都可能更好移动

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多