Python a "selenium" and ** watir ** and ** Robot Framework ** Web crawler script writing code (recommended collection)

Selenium: Python introduce automation tools and code Comments

What automated script?
Automated testing concept has broad and narrow sense; broad terms by all tools can be referred to as automated software testing measurement
test; narrow sense, refers primarily based on automated testing UI layer; in addition to the code group the preparation phase of automated testing unit,
basic integrated interface test automation testing phase.
Hereinafter called "automated testing" refer-based "UI functional test automation", (UI Test: interface testing)
currently available automated testing tools very much, here are several of the more common automated testing tools.
QTP
QTP is HP Quick Test Professional software for short, is an enterprise-level automated testing tools. It provides a powerful
and easy to use recording playback. Support B / S and C / S two architectures of software testing. It is the mainstream of automated testing tools.
Framework Robot
Robot Framework is an automated functional testing framework written a python. Good scalability, support keyword-driven,
you can simultaneously test multiple types of client interfaces or can be distributed test execution.
Watir stands for "Web Application Testing in Ruby". It is an automated functional testing tool for Web-based model.
watir is a ruby language library, use the ruby language script development.
the Selenium
the Selenium is also a tool for Web application testing, to support multi-platform, multi-browser, multi-language to automate testing.
Currently in web automation applications more widely.
Of course, in addition to the above listed test automation work outside, depending on the application, there are many commercial, open source and the company developed its own
automated testing tools.

Watir
Watir stands for "Web Application Testing in Ruby". It is an automated functional testing tool for Web-based model.
watir is a ruby language library, use the ruby language script development.

What is selenium?
selenium automated test browser, it is mainly for automated testing of Web applications, but certainly not limited to this, with the
support of all web-based administration task automation when.
selenium features:
 open source, free
 multi-browser support: FireFox, Chrome, IE, Opera
 Multi-platform support: Linux, Windows, the MAC
 Multi-language support: the Java, Python, Ruby, PHP, C #, JavaScript
 on the web page has good support
 simple (API simple), flexible (driving development language)
 support distributed test case execution
selenium experienced two versions, selenium 1.0 and selenium 2.0, selenium is not simply a tool, and
is made up of several a tool, each tool has its own characteristics and application scenarios.

In addition selenium have two versions of the first version of tools: Selenium IDE, selenium Gird, SeleniumRC
selenium IDE
selenium IDE is embedded into a Firefox browser plug-in, to achieve a simple recording and playback browser operation.
So under what circumstances does it use?
Quickly create scripts to reproduce the bug, test personnel during testing, the discovery of the bug can be reproduced by step IDE record
system down to help developers more easily reproduce bugi
In addition there is a special and good IDE useful features: You can get the code base

IDE to use and install packages address
1. After downloading IDE package file directly speak to throw in Firefox
2. After then thrown in the following picture will be prompted to Here Insert Picture Descriptionclick to install

3. Open the Firefox browser developer tools
Here Insert Picture Description
Here Insert Picture Description
to the success of this step is to install the

编写自动化脚本
有了上面的环境,你一定很迫切想要编写并运行一个自动化脚本,下面就来体验一下 python 与
wegdriver 结合之后编写的脚本是多么简洁:
如果是 windows 用户,在开始菜单找到 python 目录,打开 IDLE(python GUI)程序,启动的是一个
交互模式。可以输入:from selenium import webdriver
上面的命令为导入 selenium 的相关包,如果回车后没有报错表示我们的 selenium 安装是成功的。
图 2.4
下面通选择菜单栏 File—>New Windows 或通过快捷键 Ctrl+N 打开新的窗口。
输入以下代码:
from selenium import webdriver
browser = webdriver.Firefox()
http://fnng.cnblogs.com 29
browser.get(“http://www.baidu.com”)
browser.find_element_by_id(“kw”).send_keys(“selenium”)
browser.find_element_by_id(“su”).click()
browser.quit()
输入完成后命令为 baidu.py 保存,按 F5 快捷键运行脚本,将看到脚本启动 Firefox 浏览器进入百度页,
输入“selenium” 点击搜索按钮,最后关闭浏览器的过程。(这里默认读者已经安装了 Firefox 浏览器)
我们后面的脚本也将会在这个编辑器下完成,在你还没找到更好的编辑器之前。
代码解析:
# coding = utf-8
为了防止乱码问题,以及方便的在程序中添加中文注释,把编码统一成 UTF-8。
from selenium import webdriver
导入 selenium 的 webdriver 包,只有导入 webdriver 包我们才能使用 webdriver API 进行自动化脚本
的开发。 import 所引入包,更专业的叫法为:模组(modules)
browser = webdriver.Firefox()
需要将控制的 webdriver 的 Firefox 赋值给 browser;获得了浏览器对象才可以启动浏览器,打开网
址,操作页面严肃,Firefox 是默认已经在 selenium webdriver 包里了,所以可以直接调用。当然也可
以调用 Ie 或 Chrome ,不过要先安装相关的浏览器驱动才行。
browser.get(“http://www.baidu.com”)
获得浏览器对象后,通过 get()方法,可以向浏览器发送网址。
browser.find_element_by_id(“kw”).send_keys(“selenium”)
关于页面元素的定位后面将会详细的介绍,这里通过 id=kw 定位到百度的输入框,并通过键盘方法
send_keys()向输入框里输入 selenium 。多自然语言呀!
browser.find_element_by_id(“su”).click()
这一步通过 id=su 定位的搜索按钮,并向按钮发送单击事件( click() )。
browser.quit()
退出并关闭窗口的每一个相关的驱动程序。
基操代码详解
脚本中的安装浏览器驱动
browser = webdriver.Firefox()

浏览器最大化
print “浏览器最大化”
driver.maximize_window() #将浏览器最大化显示 (driver则为一个类似变量名,给他设置一个名字)
driver.quit()#退出浏览器

我们在实际的测试工作中,有时候我们在测试时需要使用 tab 键将焦点转移到下一个元素,用于验
证元素的排序是否正确。webdriver 的 Keys()类提供键盘上所有按键的操作,甚至可以模拟一些组合建的
操作,如 Ctrl+A ,Ctrl+C/Ctrl+V 等。在某些更复杂的情况下,还会出现使用 send_keys 来模拟上下键
来操作下拉列表的情况。
#coding=utf-8
from selenium import webdriver
#引入 Keys 类包
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get(“http://www.baidu.com”)
#输入框输入内容
driver.find_element_by_id(“kw”).send_keys(“selenium”)
time.sleep(3)
#删除多输入的一个 m
driver.find_element_by_id(“kw”).send_keys(Keys.BACK_SPACE)
time.sleep(3)
#输入空格键+“教程”
driver.find_element_by_id(“kw”).send_keys(Keys.SPACE)
driver.find_element_by_id(“kw”).send_keys(u"教程")
time.sleep(3)
#ctrl+a 全选输入框内容
driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,‘a’)
#ctrl+x 剪切输入框内容
driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,‘x’)
time.sleep(3)
#输入框重新输入内容,搜索
driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,‘v’)
time.sleep(3)
#通过回车键盘来代替点击操作
driver.find_element_by_id(“su”).send_keys(Keys.ENTER)
time.sleep(3)
driver.quit() (注释:quiet 则是退出浏览器的意思
#自动输入内容
boutter.find_element_by_id(“kw”),send_keys(“学霸哥哥”)
#自动进行点击
boutter.find_element_by_id(“su”),click()
查找Web页面的元素
查找元素流程:
1.首先打开浏览器输入查找内容。(任何浏览器都行,360,搜狐)
Here Insert Picture Description
2.然后选择一个链接,右键点击并点击查看审查元素
Here Insert Picture Description
3.选择元素
Here Insert Picture Description
说到这里我就要讲一点:元素的分类
元素的分类
Web元素分为ID,name,class,tag,link,partial_link,xpath,CSS,一般在使用的时候如果找不到ID或者name,直接转换xpath或者CSS。

接下来就是一个完整的一个百度运行脚本的代码如下:
from selenium import webdriver #从selenium导入到webdriver
import time #导入时间元素工具
boutter - webdriver.Fivefox() #导入浏览器类型
boutter.get(“https://www.baidu.com”) #获取百度网址
boutter.find_element_by_id(“kw”),send_keys(“学霸哥哥”) #根据ID元素来进行查询,然后并输入“学霸哥哥”,"kw"是百度”ID“元素
time.sleep(2) #time则是在代码开头导入的时间,sleep则是在脚本运行查找时进行休息两秒,具体休息时间可以自己定)
boutter.find_element_by_id(“su”),click() #click是点击的意思,"su"则是百度最初始页面提交按钮的页面元素,根据ID来查找的
boutter.quiet() #退出环节

**虽然这些代码比较简单,别人的代码块都是直接导入的,但我自己还是毕竟喜欢手打,还是希望对于别人有所帮助,谢谢大家不喜勿喷,毕竟打字和截图不易 **

Released four original articles · won praise 22 · views 3071

Guess you like

Origin blog.csdn.net/weixin_46115826/article/details/104458530