python webdriver提供的8种定位

发布时间 2023-06-01 15:03:33作者: 云淡#风清

# webdriver提供的8种定位

在UI层面的自动化测试开发中,元素的定位与操作是基础,也是经常遇到的困难所在。webdriver提供了8种定位:

1. id定位

find_element(By.ID,"id值");id属性是唯一的

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get("https://www.baidu.com")
browser.find_element(By.ID,'kw').clear()
browser.find_element(By.ID,'kw').send_keys('selenium')
browser.find_element(By.ID,'su').click()
time.sleep(20)
browser.quit()

2. name定位

元素的名称,find_element(By.NAME,"name值");name属性值在当前页面可以不唯一

from selenium import webdriver
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.NAME,"wd").send_keys('美女')
驱动.find_element(By.ID,'su').click()
驱动.sleep(20)
驱动.quit()

find_elements_by_name("PeriodName")是因为当前页面有一组radiobutton的name值是PeriodName,所以可以用定位一组元素的方法findElements,定位出来的是结果一个list

3. class定位

元素的类名,find_element(By.CLASS_NAME,"class值")

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动 = webdriver.Chrome()
驱动.get("https://www.baidu.com/")
驱动.find_element(By.CLASS_NAME,'s_ipt').send_keys("美女")
驱动.find_element(By.ID,'su').click()
驱动.sleep(20)
驱动.quit()

4. tag定位

页面html文档下的各种标签,通过标签定义元素成功率低,标签众多不唯一,不建议使用

tag往往用来定义一类功能,所以通过tag识别某个元素的概率很低。任意打开一个页面,都会发现大量的<div><input><a>等tag,所以tag name定位很少用

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动 = webdriver.Chrome()
驱动.get("https://www.baidu.com")
驱动.find_element(By.TAG_NAME,"input")
驱动.find_element(By.ID,'su').click()

5. link定位

专门用来定位文本链接,

find_element(By.LINK_TEXT,"text");

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动 = webdriver.Chrome()
驱动.get("https://www.baidu.com")
驱动.find_element(By.LINK_TEXT,"hao123").click()
驱动.sleep(20)
驱动.quit()

driver.find_element(By.PARTIAL_LINK_TEXT,,u"退出").click()#页面右上方的一些个人操作,比如退出、个人中心、消息通知等

6. partial link定位

是对link定位的一种补充,当链接上的文本内容比较长的时候,可以取文本的一部分进行定位,当然这部分可以唯一地标识这个链接

※注:以上的方式稍有局限,且经常页面没有id,name这些属性值,class name重复性较高,link定位有针对性,所以Xpath与Css定位更灵活些。

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动 = webdriver.Chrome()
驱动.get("https://www.baidu.com")
#文本精准匹配
驱动.find_element(By.LINK_TEXT,"hao123").click()
#文本模糊匹配
驱动.find_element(By.PARTIAL_LINK_TEXT,'帮助').click()
驱动.sleep(20)
驱动.quit()

7. XPath定位

find_element(By.XPATH,"");有多种定位策略,用FirePath插件自动生成的涵盖以下几种方式

1)绝对路径定位(不建议使用)

对于没有id,name、classname不好定位的,这也是我最常用的,因为可以通过Firefox的FirePath插件可以方便的获取到xpath值

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动=webdriver.Chrome()
驱动.get("https://www.baidu.com")
#绝对路径,不建议使用
驱动.find_element(By.XPATH,'//*[@id="s-top-left"]/a[3]').click()
驱动.sleep(20)
驱动.quit()

2)利用元素属性定位:

  find_element(By.XPATH,".//[@id='Title']"),这里是用的id,也可以用元素其他能够唯一标识的属性,不局限于id、name、class这些;代表的是标签名,不指定时就可以用*代替

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动=webdriver.Chrome()
驱动.get("https://www.baidu.com")
#绝对路径,不建议使用
# 驱动.find_element(By.XPATH,'//*[@id="s-top-left"]/a[3]').click()
#利用元素属性定位:
驱动.find_element(By.XPATH,'//input[@name="wd"]').send_keys("美女")
驱动.find_element(By.XPATH,'//*[@type="submit"]').click()
驱动.sleep(20)
驱动.quit()

3)层级与属性结合:

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动=webdriver.Chrome()
驱动.get("https://www.baidu.com")
#1绝对路径,不建议使用
# 驱动.find_element(By.XPATH,'//*[@id="s-top-left"]/a[3]').click()
#2利用元素属性定位:
驱动.find_element(By.XPATH,'//input[@name="wd"]').send_keys("美女")
# 驱动.find_element(By.XPATH,'//*[@type="submit"]').click()
#3通过层级定位
# 驱动.find_element(By.XPATH,'//*[@class="bg s_btn_wr"]').click()
#4通过了逻辑运算符
驱动.find_element(By.XPATH,'//*[@type="submit" and @class="bg s_btn"]').click()
驱动.sleep(20)
驱动.quit()

4)使用逻辑运算符

1 driver.find_element(By.XPATH,".//*[@id='divword']/input[7]").click()#登录 2 driver.find_element_by_xpath("html/body/div[4]/div/div[2]/div/div[3]/a[1]").click()#个人页面的发布课程操作

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动=webdriver.Chrome()
驱动.get("https://www.baidu.com")
#1绝对路径,不建议使用
# 驱动.find_element(By.XPATH,'//*[@id="s-top-left"]/a[3]').click()
#2利用元素属性定位:
驱动.find_element(By.XPATH,'//input[@name="wd"]').send_keys("美女")
# 驱动.find_element(By.XPATH,'//*[@type="submit"]').click()
#3通过层级定位
# 驱动.find_element(By.XPATH,'//*[@class="bg s_btn_wr"]').click()
#4通过了逻辑运算符
驱动.find_element(By.XPATH,'//*[@type="submit" and @class="bg s_btn"]').click()
驱动.sleep(20)
驱动.quit()

5)通过contains连接方式

from selenium import webdriver
from selenium.webdriver.common.by import By
驱动=webdriver.Chrome()
驱动.get("https://www.baidu.com")
#1绝对路径,不建议使用
# 驱动.find_element(By.XPATH,'//*[@id="s-top-left"]/a[3]').click()
#2利用元素属性定位:
驱动.find_element(By.XPATH,'//input[@name="wd"]').send_keys("美女")
# 驱动.find_element(By.XPATH,'//*[@type="submit"]').click()
#3通过层级定位
# 驱动.find_element(By.XPATH,'//*[@class="bg s_btn_wr"]').click()
#5通过连接方式
驱动.find_element(By.XPATH,'//span[contains(@class,"s_ipt_wr ")]/input').send_keys("selenium")
#4通过了逻辑运算符
驱动.find_element(By.XPATH,'//*[@type="submit" and @class="bg s_btn"]').click()
驱动.sleep(20)
驱动.quit()

6)通过text()方法

使用text()方法用于匹配显示文本的内容

from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.XPATH,'//a[text()="hao123"]').click()
驱动.sleep(20)
驱动.quit()

8. CSS定位(薄弱,用的很少,但很强大,比xpath简洁灵活

使用选择器来为页面元素绑定属性,可以灵活地选择控件的任意属性;find_element_by_css_selector("");同样也可以用FirePATH生成css哟!

 

1)通过class属性定位

点号(".")表示通过class属性定位

1 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
2 <input type="submit" id="su" value="百度一下" class="bg s_btn">
from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.CSS_SELECTOR,'.s_ipt').send_keys("css_class")
驱动.find_element(By.CSS_SELECTOR,'.s_btn').click()
驱动.sleep(20)
驱动.quit()

2)通过id属性定位

("#")表示通过id定位元素

1 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
2 <input type="submit" id="su" value="百度一下" class="bg s_btn">
from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.CSS_SELECTOR,'#kw').send_keys("css_id")
驱动.find_element(By.CSS_SELECTOR,'#su').click()
驱动.sleep(20)
驱动.quit()

 

3)通过父标签定义子标签

("[]"),中括号里的属性可以唯一标识这个元素就可以;属性的值可以加引号,也可以不加

1 <input type="submit" id="su" value="百度一下" class="bg s_btn">
from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.CSS_SELECTOR,'#kw').send_keys("css其他定位")
驱动.find_element(By.CSS_SELECTOR,'[type="submit"]').click()
驱动.sleep(20)
驱动.quit()

4)通过组合方式

<input type="submit" id="su" value="百度一下" class="bg s_btn">
from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.CSS_SELECTOR,'#kw').send_keys("css组合定位")
驱动.find_element(By.CSS_SELECTOR,'span>input#su').click()
from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
驱动.find_element(By.CSS_SELECTOR,'#kw').send_keys("css组合定位")
驱动.find_element(By.CSS_SELECTOR,'span>[value="百度一下"]').click()

9.混合组合定位

平时使用生成的xpath,id,name,classname这些比较多,今天根据最近这段时间的实践,并参照书上整理了下,发现原来XPath和Css下还有这么多方式,顺便拿最近一些代码试验了下,有些简单的css定位能够成功,有的Firepath生成的并不可用,一些组合定位还需要再研究,是有些难度的。最后记录一种定位方式,更接近底层实现方式的定位,But书上说webdriver更推荐前面那些写法,为毛捏?

  除find_element_by_***这种方式,还有另一套写法,也就是统一调用find_element()方法,两个参数,第一个参数是定位的类型,由By提供;第二个参数是定位的具体值

from selenium.webdriver.common.by import By #使用By这种定位前要将By类导入

find_element(By.ID,"loginName")
find_element(By.NAME,"SubjectName")
find_element(By.CLASS_NAME,"u-btn-levred")
find_element(By.TAG_NAME,"input")
find_element(By.LINK_TEXT,"退出")
find_element(By.PARTIAL_LINK_TEXT,"退")
find_element(By.XPATH,".//*[@id='Title")find_element(By.CSS_SELECTOR,"[type=submit]")

例:定位一组元素

from selenium import webdriver
import time
驱动 = webdriver.Chrome()
from selenium.webdriver.common.by import By
驱动.get("https://www.baidu.com")
elements_test01=驱动.find_elements(By.CSS_SELECTOR,"#s-top-left > a")
for element in elements_test01:
   print(element.text)
   element.click()