这两天软件的一位同学,让我用python给他写一个挂机脚本。具体需求是:登录设备管理页面,进入指定页面,等待页面超时,再进行点击页面操作;之前这一块技术,自己学习过。但是坑爹的是,很长一段时间没使用,对于定位标签,竟然还需要去查找资料。此篇文章主要记录selenium相关技术,包括关于如何定位页面标签。省去以后查找资料的时间。
什么是selenium?
selenium是一个免费、开源、跨平台的自动化测试工具。selenium主要分为selenium1.0和selenium2.0,当然还有selenium3.0。
其中Selenium 1.0 = Selenium IDE + Selenium Grid + Selenium RC。Selenium RC(Remote Control)是Selenium家族的核心部分。Selenium RC 支持多种不同语言编写的自动化测试脚本,通过Selenium RC的服务器作为代理服务器去访问应用,从而达到测试的目的。Selenium RC分为Client Libraries和Selenium Server。Client Libraries库主要用于编写测试脚本,用来控制Selenium Server的库。Selenium Server负责控制浏览器行为。
webdriver与selenium本身并不属于同一个项目,但后面两者的合并也促使selenium2.0的诞生。所以selenium2.0=selenium1.0+webdriver。webdriver可以看作是selenium RC的替代品。
1
| Python(webdriver)=>Chromedriver=>Chrome
|
安装软件环境
安装python3,进入python官网,选择python3进行下载。注意:windows平台安装python时d,需将添加到环境变量的选项勾选。
安装selenium包,cmd或者终端中输入如下命令
下载webdriver,点击进入淘宝npm。有人会问,为什么不用npm?因为墙的缘故。点击ChromeDriver镜像选项,下载Chromedriver。注意chromedriver.exe需要与chrome浏览器版本匹配。下载得到压缩包后,将解压得到的Chromedriver.exe放到python的环境变量路径下。
Mac平台下环境变量设置
使用如下命令查看MAC的环境变量
通过命令将Chromedriver复制至/usr/local/bin目录下即可
1
| cp /Users/fanhao/Downloads/chromedriver /usr/local/bin
|
Windows平台下环境变量设置
如果之前安装python3时,已经勾选了添加到path中,则将下载得到Chromedriver.exe放到python3的路径下。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| from selenium import webdriver import time driver=webdriver.Chrome() driver.get("http://192.168.1.1") n=199 while n>0: n=n-1 print(n) driver.find_element_by_xpath("//input[@id='login-username']").send_keys("admin") driver.find_element_by_xpath("//input[@id='login-password']").send_keys("password") driver.find_element_by_xpath("//input[@id='submit']").click() driver.find_element_by_xpath("//a[@href='./main.html#info']").click() driver.switch_to_frame("menufrm") driver.find_element_by_xpath("//a[@href='dslatm.cmd']").click() time.sleep(300) driver.find_element_by_xpath("//a[@href='wancfg.cmd']").click() driver.refresh()
|
定位元素的方法
绝对定位:
此方法最为简单,具体格式为
1
| driver.find_element_by_xpath("绝对路径")
|
具体例子:
1 2
| driver.find_element_by_xpath("/html/body/div[x]/form/input")
|
此方法缺点显而易见,当页面元素位置发生改变时,都需要修改,因此,并不推荐使用
相对定位:
相对路径,以‘//’开头,具体格式为
1
| driver.find_element_by_xpath("//标签")
|
具体例子:
1 2
| driver.find_element_by_xpath("//input[x]")
|
相对路径的长度和开始位置并不受限制,也可以采取以下方法
1 2
| driver.find_element_by_xpath("//div[x]/form[x]/input[x]")
|
标签加属性定位:
相对比较简单,也要求属性能够定位到唯一一个元素,如果存在多个相同条件的标签,默认只是第一个,具体格式如下
1 2
| driver.find_element_by_xpath("//标签[@属性==‘属性值’]") driver.find_element_by_xpath("//input[@type='name' and @name='kw1']")
|
xpath:模糊匹配
以定位百度页面的超链接“hao123”为例
1 2 3 4 5 6 7 8
| driver=webdriver.Chrome() driver.get("https://www.baidu.com")
driver.find_element_by_xpath("//*[contains(text(),'hao123')]").click()
driver.find_element_by_xpath("//*[contains(@id,'kw')]").send_keys("xpath")
driver.find_element_by_xpath("//*[starts-with(@id,'kw')]").send_keys("test")
|
附录
python相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from selenium import webdriver driver=webdriver.Chrome() driver.get("http://192.168.100.23/test.html")
driver.find_element_by_xpath("//input[@data-name]").send_keys('test')
driver.find_element_by_xpath("//radio[@data-male]").click() time.sleep(1) driver.find_element_by_xpath("//radio[@data-female]").click()
driver.find_element_by_xpath("//checkbox[@data-bike]").click() driver.find_element_by_xpath("//checkbox[@data-car]").click()
driver.find_element_by_xpath("//select[@data-select]").click()
driver.find_element_by_xpath("//textarea[@data-textarea]").send_keys('Te')
driver.find_element_by_xpath("//a[@data-href]").click()
driver.find_element_by_xpath("//input[@data-divinput]").send_keys('test')
driver.find_element_by_xpath("//input[@data-submit]").click()
|
ruby相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| require 'watir-webdriver' b=Watir::Browser.start("192.168.100.23/test.html",:chrome) b.text_field(:xpath=>'//input[@data-name]').send_keys('test')
b.radio(:xpath=>'//input[@data-female]').click() b.input(:xpath=>'//input[@data-female]').click()
b.checkbox(:xpath=>'//input[@data-bike]').set()
b.select(:xpath=>'//select[@data-select]').select_value('saab')
b.select(:xpath=>'//select[@data-select]').select('Audi')
b.textarea(:xpath=>'//textarea[@data-textarea]').send_keys('TTTTT')
b.a(:xpath=>'//a[@data-href]').click()
b.button(:xpath=>'//input[@data-submit]').click() b.input(:xpath=>'//input[@data-submit]').click()
|