首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >尝试使用Selenium Python自动化凭证代码输入

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

Stack Overflow用户
提问于 2016-08-28 02:12:17
回答 1查看 454关注 0票数 0

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

如果出现错误,请在索尼网站的账户管理中-Generates a code -Logs at redeem -Enters -Checks处的代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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,我会得到错误消息"Error found“,然后它会崩溃,并给我这样的结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-28 03:06:46

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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的操作:执行页面上没有的其他操作,因此退出或重新加载。更重要的是,搬家可能更好。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
voucher_box = driver.find_element_by_id("voucherCode")
          redeem_button = driver.find_element_by_id("redeemGiftCardButton")

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39187438

复制
相关文章
python selenium 实现自动化输入账号 word 邮箱
import time from selenium import webdriver from selenium.webdriver.common.by import By '''创建谷歌浏览器对象
刘鸿运
2022/11/22
5750
Python Selenium自动化详解
Selenium,Python的浏览器自动化大佬库,称霸Python浏览器自动化领域。 作为萌新的我,当然要先学习这个既简单又困难的库。
NikoDos
2022/03/29
6550
Python Selenium自动化详解
Python Selenium 自动化详解
注意,阅读本文需要有亿点点前端知识才容易理解。要是大佬看到了不会冒犯到吧,不会吧……
NikoDos
2022/04/20
6510
Python Selenium 自动化详解
python selenium 实现自动输入信息报名
import time from selenium import webdriver from selenium.webdriver.common.by import By import seleni
刘鸿运
2022/11/22
4430
Selenium2+python自动化34-获取输入框联想词
前言 最近有小伙伴问百度输入后,输入框下方的联想词如何定位到,这个其实难度不大,用前面所讲的元素定位完全可以定位到的。 本篇以百度输入框输入关键字匹配后,打印出联想词汇。 一、定位输入框联想词 1.首
上海-悠悠
2018/04/08
1.4K0
Selenium2+python自动化34-获取输入框联想词
Selenium2+python自动化69-PhantomJS使用
前言 PhantomJS是一个没有界面的浏览器,本质上是它其实也就是一个浏览器,只是不在界面上展示。 PhantomJS非常适合爬虫方面,很多玩爬虫的都喜欢用这个浏览器。 一、PhantomJS环境
上海-悠悠
2018/04/08
6590
Selenium2+python自动化69-PhantomJS使用
Selenium2+python自动化4-pycharm使用
前言 在写脚本之前,先要找个顺手的写脚本工具。python是一门解释性编程语言,所以一般把写python的工具叫解释器。写python脚本的工具很多,小编这里就不一一列举的,只要自己用着顺手就可以的,如果你还没有选好解释器,小编这里推荐pycharm。 在安装pycharm后,有一些小伙伴不会破解,这里小编还是推荐大家买正版的。当然,如果你不想付费,想破解pycharm,也是很容易的事情,这里小 编列 举几种破解办法。前提是你要先下载pycharm安装包,安装包可以去官网http
上海-悠悠
2018/04/08
9070
Selenium2+python自动化4-pycharm使用
Selenium自动化:代码测试与无代码测试
大多数测试人员认为Selenium是满足其测试自动化需求的自动化框架。作为全球测试人员使用的开放源框架,Selenium无疑是测试人员适应日趋敏捷的公司的一种好方法。实际上,Selenium仍然被认为是最普遍的开源代码,已在世界范围内采用,并且用户群体不断增长。
FunTester
2020/07/28
6870
selenium与python自动化测试
将一些必须必要条件准备好 1,安装chorme浏览器 1,安装依赖 sudo apt-get install libxss1 libappindicator1 libindicator7 2,下载google安装包 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 3,安装 sudo dpkg -i google-chrome*.deb sudo apt-get install -f 2,安装chor
py3study
2020/01/19
6620
selenium-python自动化测试
对web进行自动化测试的知识基本介绍的差不多了,接下来的时间,我会依次更新appium的部分,考虑到之前web部分介绍的有点零散,准备先把这部分知识进行整合到百度阅读,这样看即方便,又对之前的知识有一个系统的整理和梳理,链接地址为:http://yuedu.baidu.com/ebook/3c0077aaa32d7375a41780bb,见截图:
无涯WuYa
2018/10/25
4920
selenium-python自动化测试
Python+selenium 自动化-selenium自带的截图功能
selenium 自带的截图方法有两个。 方法一: save_screenshot() 方法二: get_screenshot_as_file() 用法一样,都是截取浏览器当前窗口里的内容。
小蓝枣
2020/09/23
7200
python selenium短信轰炸代码
代码可以理解为两方面。一部分是借助python的selenium库来实现自动化与网页交互,另一部分是借助网站平台在登录时,可以通过发送手机验证码来实现,
用户9325455
2021/12/25
10K0
python selenium短信轰炸代码
使用Python和Selenium库实现饭圈自动化投票
饭圈文化是一种由热爱和支持自己喜欢的偶像所构成的文化。在这个文化中,粉丝们通常会通过多种方式来表达他们的爱意,例如关注偶像参与的综艺和电视剧,使用各种社交平台为偶像打榜投票,以争取让偶像获得更高的排名和更多的曝光。
jackcode
2023/04/18
7030
使用Python和Selenium库实现饭圈自动化投票
爬虫入门经典(十四) | 使用selenium尝试爬取豆瓣图书
  大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—不温不火,本意是希望自己性情温和。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己
不温卜火
2020/11/03
6810
爬虫入门经典(十四) | 使用selenium尝试爬取豆瓣图书
Selenium2+python自动化72-logging日志使用
前言 脚本运行的时候,有时候不知道用例的执行情况,这时候可以加入日志,这样出现问题后方便查阅,也容易排查哪些用例执行了,哪些没有执行。 一、封装logging模块 1.关于logging日志的介绍,我这里就不详细讲解了,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 2.先封装logging模块,保存到common文件夹命名为logger.py,以便于调用 # coding:utf-8 import logging,time,os # 这个是日志保存本地的路径 log_path = "D:\\te
上海-悠悠
2018/04/08
1.1K0
Selenium2+python自动化72-logging日志使用
Python:Selenium 2:使用
WebDriver在将控制权返回给测试脚本之前,会一直等待到页面完全加载完毕,但如果页面使用了很多AJAX,WebDriver可能无法准确判断页面是否完全加载。
丹枫无迹
2019/09/11
2K0
Git-Svn保存密码凭证免输入
作者:matrix 被围观: 1,002 次 发布时间:2021-08-12 分类:兼容并蓄 | 无评论 »
HHTjim 部落格
2022/09/26
1.6K0
Git-Svn保存密码凭证免输入
python selenium xpath使用
/ 表示绝对路径,绝对路径是指从根目录开始 //表示相对路径 .表示当前层 ..表示上一层 *表示通配符 @表示属性 []属性的判断条件表达式
用户5760343
2022/05/13
9130
Selenium2+python自动化27-查看selenium API
前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就
上海-悠悠
2018/04/08
7460
Selenium2+python自动化27-查看selenium API
点击加载更多

相似问题

正在尝试查找xpath以使python/selenium自动化

10

使用selenium python自动化SoundCloud

112

Python Selenium自动化

12

尝试减少Python selenium代码中的代码重复

20

如何使用Selenium自动化电子签名输入?

190
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文