Python 3 + Selenium 3 实现汉堡王客户调查提交
用Python 3 + Selenium 3实现汉堡王客户调查的自动填写,可以用来作为 python selenium的入门学习实现脚本,列举了几个比较不太好弄的知识点。
上代码:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import unittest
from selenium.webdriver.common.keys import Keys
import time class TellBK(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30) def test_fillFeedBack(self):
driver = self.driver
driver.maximize_window()
driver.get('https://tellburgerking.com.cn')
time.sleep(10) # Page 1 - Welcome
# 点击继续
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@id="NextButton"]')))
element.click() # Page 2 - Fill the codes and continue
# 填写调查代码, FeedbackCode.txt保存调查代码,3个一组,空格分开
codefile = open('FeedbackCode.txt', 'r')
feedbackcode = codefile.read().split(" ")
codefile.close()
for i in range(len(feedbackcode)):
driver.find_element_by_xpath('//input[@id="CN{}"]'.format(i+1)).send_keys(feedbackcode[i])
# 点击开始
driver.find_element_by_xpath('//input[@id="NextButton"]').submit() # Page 3 - Page 12
radiovaluelist = [('simpleInput rblv', 2), ('simpleInput rblv', 2), ('simpleInput rblv', 1),
('simpleInput rbl', 5), ('simpleInput rbl', 5), ('simpleInput rbl', 5),
('simpleInput rbl', 5), ('simpleInput rbl', 9), ('simpleInput rbl', 2),
('simpleInput rbl', 5)]
for radiovalue in radiovaluelist:
self.selectRadiosSubmit(radiovalue) # Page 13 - Say something
with open('HappyReason.txt') as filereason:
reasontext = filereason.read()
driver.find_element_by_xpath('//textarea[@id="S000122"]').send_keys(reasontext)
# Next
driver.find_element_by_xpath('//input[@id="NextButton"]').submit() # Page 14 - What you ordered
element = self.driver.find_element_by_id("R000091")
self.driver.execute_script("arguments[0].click();", element)
driver.find_element_by_xpath('//input[@id="NextButton"]').submit() element = self.driver.find_element_by_id("R000097")
self.driver.execute_script("arguments[0].click();", element)
driver.find_element_by_xpath('//input[@id="NextButton"]').submit() # Page 16 - Page 19
radiovaluelist = [('simpleInput rbl', 1), ('simpleInput rblv', 5),
('simpleInput rblv', 2), ('simpleInput rblv', 1), ]
for radiovalue in radiovaluelist:
self.selectRadiosSubmit(radiovalue) # Page - Gender and age
driver.find_element_by_xpath("//select[@id='R069000']").find_element_by_xpath("//option[@value='2']").click()
time.sleep(3)
driver.find_element_by_xpath("//select[@id='R070000']").find_element_by_xpath("//option[@value='3']").click()
driver.find_element_by_xpath('//input[@id="NextButton"]').submit()
time.sleep(3) # Page - Share zip code
driver.find_element_by_xpath('//input[@id="NextButton"]').click()
time.sleep(10) # Page - Last page get screenshot
driver.get_screenshot_as_file('%s.png' % time.strftime("%Y%m%d.%H.%M.%S")) def selectRadiosSubmit(self, radioattribute):
elements = self.driver.find_elements_by_xpath(
'//input[@class="{}" and @value="{}"]'.format(radioattribute[0], radioattribute[1]))
print(len(elements))
for element in elements:
self.driver.execute_script("arguments[0].click();", element)
time.sleep(3)
self.driver.find_element_by_xpath('//input[@id="NextButton"]').submit()
time.sleep(3) def tearDown(self) -> None:
self.driver.quit() if __name__ == "__main__":
unittest.main()
需要注意的几个问题:
- 汉堡王客户调查页面的Radio是没办法直接调用click的, 会抛‘could not be scrolled into view’ Exception.需要调用Javascript进行点击。没有试ActionChains可否操作, 同样的问题对于checkbox也是,需要调用Javascript进行点击。
- 汉堡王客户调查页面的Radio也是有区别的,有的class = 'simpleInput rbl', 有的class = 'simpleInput rblv'
- 页面上多组Radio的ID值是变动的,不能直接使用,所以用了class 加 value定位。原本我打算用ends-with的,但是试了好像不行。这个ends-with对于xpath的定位比较弱,不太好匹配上,即使你觉得本应该没问题, 不建议使用。
- selenimu的上脚本点击的速度是远超过页面响应速度的,所以必要的时候,要强制time.sleep(), 或者使用WebDriver的显示等待。建议每个action后都稍微sleep下,特别是页面跳转的时候。对于关键元素WebDriver进行check
- 这些页面里面没有iframe,所以你不会碰到iframe导致的元素找不到的问题。
- 还需要注意button上的click()和submit()方法是略有不同的。click()对于button来说都可以使用,submit()用于在点击button后提交表单数据,使用需要满足一定的条件, 比如按钮的type='submit',而且这个按钮要在<form> tag里面。
- 我没有添加exception的处理,这里的代码只是基础代码,可以自行优化,以及组织。
- 最后的验证码是保存在一个png文件里面的,需要自己手动去查看。没来得及是实现和抓取。
Python 3 + Selenium 3 实现汉堡王客户调查提交的更多相关文章
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...
- [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈
我自认为这是自己写过博客中一篇比较优秀的文章,同时也是在深夜凌晨2点满怀着激情和愉悦之心完成的.首先通过这篇文章,你能学到以下几点: 1.可以了解Python简单爬取图片的一些思路和方法 ...
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- 【Python】 Selenium 模拟浏览器 寻路
selenium 最开始我碰到SE,是上学期期末,我们那个商务小组做田野调查时发的问卷的事情.当时在问卷星上发了个问卷,但是当时我对另外几个组员的做法颇有微词,又恰好开始学一些软件知识了,就想恶作剧( ...
- Python 配置 selenium 模拟浏览器环境,带下载链接
使用浏览器渲染引擎.直接用浏览器在显示网页时解析HTML,应用CSS样式并执行JavaScript的语句. 这方法在爬虫过程中会打开一个浏览器,加载该网页,自动操作浏览器浏览各个网页,顺便把数据抓下来 ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...
随机推荐
- MySQL使用alter修改表的结构
SQL语句 DLL 数据定义语言 create,drop DML 数据操纵语言 insert,delete,select,upda ...
- postgresql设置max_connections太大无法启动 (转载)
本篇随笔转载自https://my.oschina.net/u/2381678/blog/552346. 在生产环境postgresql中,需要调整最大链接数,但是调整后无法启动 错误的意思就是内核中 ...
- django rest framework 解析器组件 接口设计,视图组件 (2)
1. 使用视图组件进行接口优化 1.1 使用视图组件的mixin进行接口逻辑优化 - 导入mixin from rest_framework.mixinx import ( ListModelMix, ...
- flask的客户端服务端
1.首先要进行后端与前端的连接有get 和post请求 get请求是直接在网页上打出已将定义好的网址 if __name__ == '__main__': app.run(host="loc ...
- vue-router路由嵌套与二级路由重定向
(1)公共路由裁减 不是每个页面都存在导航,所以不要把导航组件在根组件APP.vue里引入,哪个页面需要,在哪里引入即可. 方案: 哪个页面需要,在哪个页面引入即可 (2)嵌套路由 注意:childr ...
- @TableName(mybatis-plus中的注解)
@TableName 描述:表名注解 属性 类型 必须指定 默认值 描述 value String 否 "" 表名 schema String 否 "" sch ...
- maven 配置文件
<properties> <project.builder.sourcesEncoding>UTF-8</project.builder.sourcesEncoding& ...
- 关于python中lambda 函数使用小结
例子: 如果定义普通函数,一般都是这样写: def:ds(x): return 2*x+1 调用即: ds(5) 如果用lambda函数就是这么写,就是一句话: g =lambda x:2*x+1 调 ...
- django 导出xls文件
1.同目录下创建file_handle.py文件 file_handle.py import xlwt, datetime from xlwt import * import xlsxwriter # ...
- 禁用wordpress模板默认样式
我们知道wordpress主题比如twentytwenty都会有样式,如果不想使用它们的默认样式怎么处理呢?其实很简单,随ytkah一起来看看吧.进入2020主题的function.php文件,里面有 ...