当前位置:网站首页>webdriver.定位元素
webdriver.定位元素
2022-07-31 05:19:00 【欧粒粒】
find_element
一、id(如果存在,全局唯一)
二、name(存在,且全局唯一)
三、tag_name (需全局唯一)
四、class_name(需全局唯一)
五、link text(必须是链接,需全局唯一)
六、partial link text
七、xpath(!任何元素都可以定位到!)
八、CSS selector
一、通过id定位,全局唯一,可以唯一定位一个元素
find_element_by_id
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
time.sleep(3)
driver.find_element_by_id("kw").send_keys("猫和老鼠")
time.sleep(3)
driver.find_element_by_id("su").click()
time.sleep(3)
driver.close()- 右键,点击检查

2.找到id

二、通过name定位,name有可能不全局唯一
driver.find_element_by_name("wd").send_keys("小猫咪")from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
time.sleep(3)
# 通过id定位
# driver.find_element_by_id("kw").send_keys("猫和老鼠")
# time.sleep(3)
# driver.find_element_by_id("su").click()
# 通过name定位
driver.find_element_by_name("wd").send_keys("小猫咪")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.quit()同上,通过检查,查找name
![]()
找到,为wd
三、通过tag_name定位
同理
四、class_name
注意:必须保证全局唯一!!!
七、xpath
直接右键复制xpath

driver.find_element_by_xpath("//*[@id='form']").send_keys("梦华录")八、
同(七)上,复制,得到 #form

driver.find_element_by_css_selector("#form").send_keys("火锅")
边栏推荐
- 【内网开发日记】用websocket手搓一个聊天软件
- 一种用QT实现即时通信软件表情发送与接收的思路
- ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
- C语言对文件的操作(完整版)
- IDEA概述和安装及调试
- cenos7安装cmake-3.22.2
- ROS subscription to multiple topics time synchronization problem
- Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
- Log jar package conflict, and its solution
- Incredibuild 宣布支持 Yocto
猜你喜欢

变更管理与 DevOps —— 二者同时进行吗?

Incredibuild 宣布支持 Yocto

数据分析之SQL面试真题

Learn how to get a database connection with JDBC

Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....

【Rhapsody学习笔记】3:Dishwasher

软链接和硬链接画图,以及代码,一级目录的解释,重定向,创建文件,删除文件,创建目录,删除目录,cp、mv命令的使用
![[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config](/img/53/8b5a12e7ed551dca52ada5dbb5d6b5.png)
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config

TCP与UDP协议的区别,以及TCP的三次握手和四次挥手

UR3机器人运动学分析之正运动学分析
随机推荐
虚拟机查看端口号进程
Pytorch study notes 7 - processing input of multi-dimensional features
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
Learn how to get a database connection with JDBC
911崩了,自养号测评环境IP有哪些更好的选择
Log jar package conflict, and its solution
数据分析之SQL面试真题
离线安装activeMq
在 AWS 上从零开始设置 Incredibuild 构建系统
【Rhapsody学习笔记】2:Count Down
C语言结构体(必须掌握版)
C语言数组的深度分析
Session和Cookie,Token
关于Iframe
ES6-class类
Solution for MySQL The table is full
解决background-size:cover时图片铺满但显示不完整?
The solution to the IDEA console not being able to enter information
力扣.找到打字符串中所有字母异位词
cenos7安装cmake-3.22.2