selenium(一)简介以及元素定位

selenium简介以及元素定位

一、selenium简介

selenium是企业主流应用广泛web自动化测试框架
selenium的三大组件:
1、selenium IDE 浏览器插件:实现脚本录制
2、WebDriver 实现对浏览器的各种操作(API包)
3、Grid 分布式执行,用例同时在多个浏览器同时执行,提搞测试效率

二、selenium原理

启动浏览器驱动(chromedriver.exe)服务后,启动浏览器驱动(chromedriver.exe)服务,不同的指令对浏览器进行不同的操作。

三、元素的定位

通用代码

from selenium import webdriver
driver=webdriver.Chrome()
driver.get("http://www.baidu.com")

1.通过元素的ID属性定位

el=driver.find_element_by_id("kw")

2.通过元素的name属性定位

el=driver.find_element_by_name("wd")

3.通过标签定位

el=driver.find_element_by_tag_name("input")

4.通过class属性定位

el=driver.find_element_by_class_name("s_ipt")

5.通过链接文本定位

el=driver.find_element_by_link_text("新闻")

6.通过部分链接文本定位

el=driver.find_element_by_partial_link_text("新")

7.通过Xpath定位
1)通过绝对路径定位

el=driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input")

2)通过相对路径定位 ------//开头 经常用到的方法

el=driver.find_element_by_xpath("//form/span/input")

3)标签+索引=唯一定位目标标签

el=driver.find_element_by_xpath("//form/span[1]/input")

4)唯一定位标签+属性

el=driver.find_element_by_xpath("//form[@id='form']/span[1]/input[@id='kw']")

5)唯一定位标签+多个属性

el=driver.find_element_by_xpath("//form[@id='form'and @name='f']/span[1]/input[@id='kw']")

6)标签+部分属性定位

el=driver.find_element_by_xpath("//form/span[1]/input[substring(@class,3)='ipt']")

el=driver.find_element_by_xpath("//form/span[1]/input[contains(@id,'kw')]")

el8=driver.find_element_by_xpath("//form/span[1]/input[starts‐with(@id,'k')]")

7)通过属性定位

el=driver.find_element_by_xpath('//*[@id="kw"]')

8)通过文本定位 ------针对a标签

el=driver.find_element_by_xpath("//a[text()='新闻']")

8.通过css样式定位
1)通过绝对路径定位

el=driver.find_element_by_css_selector("html body div div div div div form span input ")

el2=driver.find_element_by_css_selector("html>body> div> div> div> div >div> form> span> input ")

2)通过id定位----#id属性值

el=driver.find_element_by_css_selector("#kw")

3)通过class属性定位 ----.class属性值

el=driver.find_element_by_css_selector('.s_ipt')

4)通过其他属性定位,并且多个属性定位

el=driver.find_element_by_css_selector("[autocomplete='off']")

el=driver.find_element_by_css_selector("[autocomplete='off'][class='s_ipt']")

5)通过标签定位 标签名+属性/id/class进行定位 组合定位

el1=driver.find_element_by_css_selector("input#kw")

el2=driver.find_element_by_css_selector("input.s_ipt")

el3=driver.find_element_by_css_selector("input[autocomplete='off'])

6)通过层级定位 层级之间通过>或者空格隔开 相对路径

el=driver.find_element_by_css_selector("form#form>span>input#kw")

7)通过兄弟节点定位
第一个元素 标签:first‐child
第一个元素 标签:first‐child
最后元素 标签:last‐of‐type

el1=driver.find_element_by_css_selector("div#s‐top‐left>a:first‐child")

el2=driver.find_element_by_css_selector("div#s‐top‐left>a:nth‐child(3)")

el3=driver.find_element_by_css_selector("div#s‐top‐left>a:last‐of‐type")

通过源码可以发现 上述查找元素的方法都是来自find_element()方法的封装

from selenium import webdriver
from selenium.webdriver.common.by import  By
driver=webdriver.Chrome()
driver.get("http://www.baidu.com")
el=driver.find_element(By.ID,"kw")
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值