selenium定位方法

image
一、打开f12 或者fn+f12  打开开发者工具
image
二、定位方法
(1)id定位 find_element_by_id()
image
(2)name定位 find_element_by_name() 
image
image

(3)class定位 find_element_by_class_name()
image

(4)link定位 find_element_by_link_text() 
image
image
image
(5)partial link定位
find_element_by_partial_link_text()  模糊定位标签
image

(6)JavaScript定位 
document.getElementById("kw").value="js定位"
image

(7)tag定位
find_element_by_tag_name()
image
image

(8)xpath定位
find_element_by_xpath()
打开元素右键copy==copy  xpth
如://*[@id="chat-textarea"]
image

(9)css定位
find_element_by_css_selector()
打开元素右键copy==copy  selector 
css:#chat-textarea
image

规则:
在这9种常用的定位方法中,优先顺序
1)有id优先使用id定位
2)没有id,考虑使用name或者class定位。
3)如果没有id,name,class再考虑用xpath,css定位。
4)如果是链接可以考虑使用link_text,partial_link_text 定位。
5)tag_name和JavaScript还是用的比较少的。我们根据实际情况,具体问题具体分析

posted @ 2026-05-18 18:02  刘sir金牌讲师  阅读(16)  评论(0)    收藏  举报