我们还可以通过Seleniun测试框架中的By类,来实现页面中的元素定位。

1、使用By定位的前提

需要导入By类:

from selenium.webdriver.common.by import By

2、By定位的方法

# 1. 通过元素的id属性进行定位。
find_element(By.ID, "id属性值") # 2. 通过元素的name属性进行定位。
find_element(By.NAME, "name属性值") # 3. 通过元素的class属性进行定位。
find_element(By.CLASS_NAME, "class属性值") # 4.通过元素标签进行定位。
find_element(By.TAG_NAME, "标签名") # 5. 通过超链接中全部文字定位超链接。
find_element(By.LINK_TEXT, "完整超链接文本") # 6. 通过超链接中部分连续文字定位超链接。
find_element(By.PARTIAL_LINK_TEXT, "部分超链接文本") # 7. 通过XPath定位元素。
find_element(By.XPATH, "XPath路径表达式") # 8. 通过css选择器定位元素。
find_element(By.CSS_SELECTOR, "css选择器定位策略")

By定位与8种基本定位方法类比

基本定位方法 等同于By定位
by_id find_element(By.ID," ") or ("id"," ")
by_name find_element(By.NAME," ") or ("name", " ")
by_class_name find_element(By.CLASS_NAME," ") or ("class name", " ")
by_tag_name find_element(By.TAG_NAME," ") or ("tag name", " ")
by_link_text find_element(By.LINK_TEXT," ") or ("link text", " ")
by_partial_link_text find_element(By.PARTIAL_LINK_TEXT," ") or ("partial link text", " ")
by_xpath find_element(By.XPATH," ") or ("xpath", " ")
by_css_selector find_element(By.CSS_SELECTOR," ") or ("css selector", " ")

这种方式只要统一写find_element(by_***,"字符串")就好,也很方便。看个人习惯使用。

3、By定位的使用

页面代码片段如下

<!DOCTYPE html>
<html lang="zh-cn">
<body>
<div id="zc">
<legend>注册用户A</legend>
<p id="p1">
<label for="userA">账号A</label>
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
</p>
<p>
<label for="password">密码A</label>
<input type="password" name="passwordA" id="passwordA" placeholder="密码A" value="">
</p>
<p><a href="http://www.sina.com.cn" id="fwA">访问 新浪 网站</a></p>
<input type="emailA" name="emailA" id="emailA" placeholder="电子邮箱A" value="">
</div>
</body>
</html>

脚本代码

# 1.导入selenium
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
import os # 2.打开浏览器
driver = webdriver.Chrome() # 3.打开页面
url = "file:///" + os.path.abspath("./1.html")
driver.get(url)
sleep(2) # 4.定位目标元素
# 4.1 通过id属性定位页面中账号A输入框
# 写法一
element_id_1 = driver.find_element(By.ID, "userA")
print(element_id_1.get_attribute("outerHTML"))
# 写法二
element_id_2 = driver.find_element("id", "userA")
print(element_id_2.get_attribute("outerHTML")) # 4.2 通过name属性定位页面中账号A输入框
# 写法一
element_name_1 = driver.find_element(By.NAME, "userA")
print(element_name_1.get_attribute("outerHTML"))
# 写法二
element_name_2 = driver.find_element("name", "userA")
print(element_name_2.get_attribute("outerHTML")) # 4.3 通过class属性定位页面中账号A输入框
# 写法一
element_class_1 = driver.find_element(By.CLASS_NAME, "c_uA")
print(element_class_1.get_attribute("outerHTML"))
# 写法二
element_class_2 = driver.find_element("class name", "c_uA")
print(element_class_2.get_attribute("outerHTML")) # 4.4 通过tag_name定位页面中账号A输入框
# 写法一
element_tag_name_1 = driver.find_element(By.TAG_NAME, "input")
print(element_tag_name_1.get_attribute("outerHTML"))
# 写法二
element_tag_name_2 = driver.find_element("tag name", "input")
print(element_tag_name_2.get_attribute("outerHTML")) # 4.5 通过link_text定位页面中超链接
# 写法一
element_link_text_1 = driver.find_element(By.LINK_TEXT, "访问 新浪 网站")
print(element_link_text_1.get_attribute("outerHTML"))
# 写法二
element_link_text_2 = driver.find_element("link text", "访问 新浪 网站")
print(element_link_text_2.get_attribute("outerHTML")) # 4.6 通过partial_link_text定位页面中超链接
# 写法一
element_partial_link_text_1 = driver.find_element(By.PARTIAL_LINK_TEXT, "问 新浪")
print(element_partial_link_text_1.get_attribute("outerHTML"))
# 写法二
element_partial_link_text_2 = driver.find_element("partial link text", "问 新浪")
print(element_partial_link_text_2.get_attribute("outerHTML")) # 4.7 通过XPath定位页面中账号A输入框
# 写法一
element_xpath_1 = driver.find_element(By.XPATH, "//input[@id='userA']")
print(element_xpath_1.get_attribute("outerHTML"))
# 写法二
element_xpath_1 = driver.find_element("xpath", "//input[@id='userA']")
print(element_xpath_1.get_attribute("outerHTML")) # 4.8 通过css_selector定位页面中账号A输入框
# 写法一
element_css_selector_1 = driver.find_element(By.CSS_SELECTOR, "input#userA")
print(element_css_selector_1.get_attribute("outerHTML"))
# 写法二
element_css_selector_2 = driver.find_element("css selector", ".c_uA")
print(element_css_selector_2.get_attribute("outerHTML")) # 5.关闭浏览器
driver.quit() """
输出结果:
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<a href="http://www.sina.com.cn" id="fwA">访问 新浪 网站</a>
<a href="http://www.sina.com.cn" id="fwA">访问 新浪 网站</a>
<a href="http://www.sina.com.cn" id="fwA">访问 新浪 网站</a>
<a href="http://www.sina.com.cn" id="fwA">访问 新浪 网站</a>
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
"""

4、复数形式的示例

(1)语法

同单数差不多,就是find_element换成了find_elements,结果会返回一个元素列表结果集<list>

# 1. 通过元素的id属性进行定位。
find_elements(By.ID, "id属性值") # 2. 通过元素的name属性进行定位。
find_elements(By.NAME, "name属性值") # 3. 通过元素的class属性进行定位。
find_elements(By.CLASS_NAME, "class属性值") # 4.通过元素标签进行定位。
find_elements(By.TAG_NAME, "标签名") # 5. 通过超链接中全部文字定位超链接。
find_elements(By.LINK_TEXT, "完整超链接文本") # 6. 通过超链接中部分连续文字定位超链接。
find_elements(By.PARTIAL_LINK_TEXT, "部分超链接文本") # 7. 通过XPath定位元素。
find_elements(By.XPATH, "XPath路径表达式") # 8. 通过css选择器定位元素。
find_elements(By.CSS_SELECTOR, "css选择器定位策略")

(2)练习

页面代码片段如下

<!DOCTYPE html>
<html lang="zh-cn">
<body>
<div id="zc">
<legend>注册用户A</legend>
<p id="p1">
<label for="userA">账号A</label>
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
</p>
<p>
<label for="password">密码A</label>
<input type="password" name="passwordA" id="passwordA" placeholder="密码A" value="">
</p>
<p><a href="http://www.sina.com.cn" id="fwA">访问 新浪 网站</a></p>
<input type="emailA" name="emailA" id="emailA" placeholder="电子邮箱A" value="">
</div>
</body>
</html>

脚本代码

# 1.导入selenium
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
import os # 2.打开浏览器
driver = webdriver.Chrome() # 3.打开页面
url = "file:///" + os.path.abspath("./1.html")
driver.get(url)
sleep(2) # 4.定位目标元素 # 4.1 通过id属性定位页面中账号A输入框
# 写法一
element_id_1 = driver.find_elements(By.ID, "userA")
for element in element_id_1:
print(element.get_attribute("outerHTML"))
# 写法二
element_id_2 = driver.find_elements("id", "userA")
for element in element_id_2:
print(element.get_attribute("outerHTML")) # 5.关闭浏览器
driver.quit() """
输出结果:
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
<input type="textA" name="userA" id="userA" class="c_uA" placeholder="账号A" required="" value="">
"""

『心善渊』Selenium3.0基础 — 9、使用Seleniun中的By类定位元素的更多相关文章

  1. 『心善渊』Selenium3.0基础 — 6、Selenium中使用XPath定位元素

    目录 1.Selenium中使用XPath查找元素 (1)XPath通过id,name,class属性定位 (2)XPath通过标签中的其他属性定位 (3)XPath层级定位 (4)XPath索引定位 ...

  2. 『心善渊』Selenium3.0基础 — 28、unittest中测试套件的使用

    目录 1.测试套件的作用 2.使用测试套件 (1)入门示例 (2)根据不同的条件加载测试用例(了解) (3)常用方式(推荐) 1.测试套件的作用 在我们实际工作,使用unittest框架会有两个问题: ...

  3. 『心善渊』Selenium3.0基础 — 24、Selenium的expected_conditions模块详细介绍

    目录 1.EC模块介绍 2.EC模块常用类 3.EC模块的使用 4.EC模块综合使用 (1)title_is(title)示例 (2)presence_of_element_located(locat ...

  4. 『心善渊』Selenium3.0基础 — 10、使用Seleniun定位页面元素归纳总结(超详细)

    目录 1.Selenium中8种基本元素定位方式 (1)单数形式 (2)复数形式 2.By类定位的8种定位方式 (1)单数形式 (2)复数形式 3.XPath定位总结 (1)基础定位语法 (2)属性定 ...

  5. 『心善渊』Selenium3.0基础 — 11、Selenium对元素常用操作

    目录 1.Selenium对元素常用操作 2.Selenium对元素的其他操作 1.Selenium对元素常用操作 操作 说明 click() 单击元素 send_keys() 模拟输入 clear( ...

  6. 『心善渊』Selenium3.0基础 — 12、Selenium操作鼠标和键盘事件

    目录 (一)对鼠标的操作 1.鼠标事件介绍 2.ActionChains 类鼠标操作的常用方法 3.perform()方法 4.鼠标事件操作步骤 5.示例 (1)右键单击.左键双击 (2)鼠标拖拽动作 ...

  7. 『心善渊』Selenium3.0基础 — 1、Selenium自动化测试框架介绍

    目录 1.Selenium介绍 2.Selenium的特点 3.Selenium版本说明 4.拓展:WebDriver与Selenium RC的区别 5.Webdriver工作原理 1.Seleniu ...

  8. 『心善渊』Selenium3.0基础 — 4、Selenium基础元素定位详解

    目录 1.什么是元素定位 2.Selenium元素定位常用API (1)By_id 定位 (2)by_name 定位 (3)by_class_name 定位 (4)by_tag_name 定位 (5) ...

  9. 『心善渊』Selenium3.0基础 — 5、XPath路径表达式详细介绍

    目录 1.XPath介绍 2.什么是XML 3.XML与HTML对比 4.为什么使用XPath定位页面中的元素 5.XPath中节点之间的关系 (1)节点的概念 (2)节点之间的关系类型 6.XPat ...

  10. 『心善渊』Selenium3.0基础 — 13、Selenium操作下拉菜单

    目录 1.使用Selenium中的Select类来处理下拉菜单(推荐) 2.下拉菜单对象的其他操作(了解) 3.通过元素二次定位方式操作下拉菜单(重点) (1)了解元素二次定位 (2)示例: 页面中的 ...

随机推荐

  1. 通过SQL注入获得网站后台用户密码

    通过 SQL 注入攻击,掌握网站的工作机制,认识到 SQL 注入攻击的防范措施,加强对 Web 攻击的防范. 一.实验环境 下载所需代码及软件:获取链接:链接:https://pan.baidu.co ...

  2. API网关才是大势所趋?SpringCloud Gateway保姆级入门教程

    什么是微服务网关 SpringCloud Gateway是Spring全家桶中一个比较新的项目,Spring社区是这么介绍它的: 该项目借助Spring WebFlux的能力,打造了一个API网关.旨 ...

  3. [bug] Hive:map.xml could only be replicated to 0 nodes instead of minReplication (=1). There are 0 datanode(s) running and no node(s) are excluded in this operation.

    原因: datanode未运行,重启hdfs

  4. ]# dmesg | grep ATAcentos下查看网卡,主板,CPU,显卡,硬盘型号等硬件信息

    centos下查看网卡,主板,CPU,显卡,硬盘型号等硬件信息 osc_4o5tc4xq 2019/10/11 15:03 阅读数 253 centos下查看网卡,主板,CPU,显卡,硬盘型号等硬件信 ...

  5. 详解Linux中的cat文本输出命令用法

    作系统 > LINUX >   详解Linux中的cat文本输出命令用法 Linux命令手册   发布时间:2016-01-14 14:14:35   作者:张映    我要评论   这篇 ...

  6. hugboy源库

    =[个人整理的一些源库,均来自网络]= -[Ubuntu]- #阿里源 Ubuntu 20.04 deb http://mirrors.aliyun.com/ubuntu/ focal main re ...

  7. 033.Python的__del__析构方法he__call__方法

    一 __del__ 魔术方法(析构方法) 1.1 介绍 触发时机:当对象被内存回收的时候自动触发[1.页面执行完毕回收所有变量 2.所有对象被del的时候] 功能:对象使用完毕后资源回收 参数:一个s ...

  8. git/repo常用命令

    Git作为广受欢迎的一款版本控制工具,它该如何通过命令行使用呢?本文为你揭晓浓缩精华精华版:git常用命令一览,含部分repo操作. 代码下载 repo init -- -->初始化需要下载的分 ...

  9. window location href is not a function(Day_36)

    报window location href is not a function错误的解决方案: 原因: JS报错是由于写法问题或浏览器不兼容导致的,具体解决方法如下: 原来报错的写法: window. ...

  10. week-02

    week-02 1.挂载一个lvm,截图给出结果 这个是之前写的,前段时间放到CSDN了 https://blog.csdn.net/weixin_43841942/article/details/1 ...