Selenium(一)之元素定位法

F12得到前端HTML代码的属性

# 由标签对组成
<html></html>

# 有各种属性 属性名为id值为head、属性名为class值为s_s_down
<div id='head' class='s_s_down'>

# 标签对之间有文本数据
<a>新闻</a>

#标签有层级关系
<html>
	<body>
	</body>
<html>

接下来以百度一下的前端代码为例讲解
在这里插入图片描述
在这里插入图片描述

WebDriver的八种元素定位方法

编号定位方法写法定位内容
1id定位find_element_by_id()id是唯一的,利用唯一性的标签定位法
2name定位find_element_by_name()利用唯一性的标签定位法
3tag定位find_element_by_tag_name()利用唯一性的标签定位法
4class定位find_element_by_class_name()利用唯一性的标签定位法
5link_text定位find_element_by_link_text()专门定位以文本显示的链接(超链接)
6partial_link定位find_element_by_partial_link_text()是对序号5link的补充,有些文字比较长,可以取部分文字进行定位
7xpath定位find_element_by_xpath()基于xpath语法的定位方法
8css定位find_element_by_css_selector()基于css语法的定位方法

1 id定位

id在html文档中必须是唯一的,所以可以通过id定位

# 普通查找
find_element_by_id(“kw”)
find_element_by_id(“su”)
# 嵌套查找
cheese = driver.find_element(By.ID, "cheese")
cheddar = cheese.find_elements_by_id("cheddar")

2 name定位

find_element_by_name("wd")

3.class 定位

find_element_by_class_name("s_ipy")

4 tag定位

find_element_by_tag_name("input")

5.link定位

定位文本的链接,定位link text可视文本与搜索值完全匹配的锚元素

find_element_by_link_name("新闻")

6.partial link定位

是对上一个link的补充,有些文字比较长,可以取部分文字进行定位
定位link text可视文本部分与搜索值部分匹配的锚点元素。如果匹配多个元素,则只选择第一个元素。

find_element_by_partial_link_text("新")

7使用Xpath定位

7.1绝对路径定位
find_element_by_xpath("/html/body/div/div[2]/div")
7.2利用元素属性定位
find_element_by_xpath("//input[@id='kw']")
7.3层级与属性结合
find_element_by_xpath("//span[@class='bg s_spit_wr']/input")
7.4使用逻辑运算符

如果一个属性不能唯一区分一个元素,那么我们可以使用逻辑运算符连接多个属性来查找元素

find_element_by_xpath("//input[@id='kw' and @class='s_ipt']")
7.5使用contains方法

contain用于匹配属性里的字符串

find_element_by_xpath("//span[contains(@class,'s_ipt_wr')]/input")
7.6使用text()方法

匹配显示文本信息,判断元素中是否存在指定的文本

find_element_by_xpath("//a[text(),'新闻']")
# 相同效果
text_to_be_present_in_element

和contain组合,是partial link的定位效果

find_element_by_xpath("//a[contain(text(),'一个很长的')]")

8.使用CSS定位

8.1使用class定位
find_element_by_css_selector(.s_ipt)
8.2使用id定位
find_element_by_css_selector('#kw')
8.3通过标签名定位
find_element_by_css_selector(‘input’)
8.4通过标签层级关系定位
find_element_by_css_selector("span > input")
8.5通过属性定位
find_element_by_css_selector("[name=''kw']")
8.6组合定位
find_element_by_css_selector("from.fm > span >input.s_sipt")
8.7更多定位用法
# 查找class属性包含s_ipt_wr的元素
find_element_by_css_selector("[class*=s_ipt_wr]")

# 查找class属性以bg字符串开头的字符
find_element_by_css_selector("[class^=bg]")

# 查找class属性以wrap字符串结尾的元素
find_element_by_css_selector("[class$=wrap]")

# 查找form标签下面第二个input标签的元素
find_element_by_css_selector("from > input:nth-child(2)")

CSS选择器的更多用法

9 一种其他写法-By定位元素

这种写法和上面的所有用处都一样,唯一是写法不同,顺序与上面八大种一一对应

新写法 find_element(By.类型,“字段”)上文八种写法
find_element(By.ID,“kw”)find_element_by_id(“kw”)
find_element(By.NAME,“wd”)find_element_by_name(“wd”)
find_element(By.CLASS_NAME,“s_sipt”)find_element_by_class_name(“s_ipy”)
find_element(By.TAG_NAME,“input”)find_element_by_tag_name(“input”)
find_element(By.LINK_TEXT,“新闻”)find_element_by_link_name(“新闻”)
find_element(By.PARTIAL_LINK_TEXT,“新”)find_element_by_partial_link_text(“新”)
find_element(By.XPATH,"//*[@CLASS=‘bg s_btn’]")find_element_by_xpath("//input[@id=‘kw’]") # 例子为7.2
find_element(By.CSS_SELECTOR,“span.bg s_btn_wr>input#su”)find_element_by_css_selector(“span > input”) # 新例子不与前面对应

多个元素定位

<ol id=cheese>
 <li id=cheddar>…
 <li id=brie>…
 <li id=rochefort>…
 <li id=camembert>…
</ol>
muchoCheese = driver.findElements(By.cssSelector("#cheese li"));

相对定位

在Selenium 4中带来了相对定位这个新功能,在以前的版本中被称之为"好友定位 (Friendly Locators)"。 它可以帮助你通过某些元素作为参考来定位其附近的元素。 现在可用的相对定位有:

  • above 元素上
  • below 元素下
  • toLeftOf 元素左
  • toRightOf 元素右
  • near 附近
    在这里插入图片描述

above() 元素上

返回当前指定元素位置上方的WebElement对象email

from selenium.webdriver.support.relative_locator import with_tag_name

passwordField = driver.find_element(By.ID, "password")
emailAddressField = driver.find_element(with_tag_name("input").above(passwordField))
  

below() 元素下

返回当前指定元素位置下方的WebElement对象

from selenium.webdriver.support.relative_locator import with_tag_name

emailAddressField = driver.find_element(By.ID, "email")
passwordField = driver.find_element(with_tag_name("input").below(emailAddressField))

toLeftOf() 元素左

返回当前指定元素位置左方的WebElement对象

from selenium.webdriver.support.relative_locator import with_tag_name

submitButton = driver.find_element(By.ID, "submit")
cancelButton = driver.find_element(with_tag_name("button").
                                   to_left_of(submitButton))

toRightOf() 元素右

from selenium.webdriver.support.relative_locator import with_tag_name

cancelButton = driver.find_element(By.ID, "cancel")
submitButton = driver.find_element(with_tag_name("button").
                                   to_right_of(cancelButton))

near() 附近

返回当前指定元素位置附近大约px5050像素的WebElement对象

from selenium.webdriver.support.relative_locator import with_tag_name

emailAddressLabel = driver.find_element(By.ID, "lbl-email")
emailAddressField = driver.find_element(with_tag_name("input").
                                       near(emailAddressLabel))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值