python中selenium各个版本的元素定位方法

查看自己的selenium版本,请参考这里。

目录

一、定位页面元素方法源码说明

(1)Webdriver.common

(2)selenium.webdriver.common.by

(3)By

二、定位页面元素方法用法汇总

(1)2.0 及以下低版本 selenium :By 定位页面元素方法用法(可忽略)

(2)3.0 ~ 3.9 版本 selenium:By 定位页面元素方法用法

(3)4.0 ~ 4.9 版本 selenium:By 定位页面元素方法用法

(4)页面元素定位方法汇总



一、定位页面元素方法源码说明

(1)Webdriver.common

Selenium Documentation

https://www.selenium.dev/selenium/docs/api/py/api.html#webdriver-common


(2)selenium.webdriver.common.by

selenium.webdriver.common.by

https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html#module-selenium.webdriver.common.by在这里插入图片描述
在这里插入图片描述


(3)By

Source code for selenium.webdriver.common.by

https://www.selenium.dev/selenium/docs/api/py/_modules/selenium/webdriver/common/by.html#By

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aQq6AWuA-1684467703698)(C:\Users\Administrator\AppData\Roaming\marktext\images\2023-05-19-11-21-39-image.png)]

Licensed to the Software Freedom
Conservancy (SFC) under one or more contributor license agreements. See the
NOTICE file distributed with this work for additional information regarding
copyright ownership. The SFC licenses this file to you under the Apache
License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at  
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or
agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing
permissions and limitations under the License.

**# ***************************************** **译文** **********************************************#**


已经根据一个或多个贡献者许可协议授权给软件自由保护协会(SFC)。
有关版权所有权的其他信息,请参见随附此工作的 NOTICE 文件。
SFC 根据 Apache License,Version 2.0(“许可证”)向您许可此文件;
除遵守许可证外,您不得使用此文件。您可以在以下网址获得许可证的副本:
http://www.apache.org/licenses/LICENSE-2.0

除非适用法律要求或书面同意,许可下分发的软件都是按 “原样” 分发的,
没有任何明示或暗示的保证或条件。请查看许可证以获得许可下的特定权限和限制。
class By:"""By 实现源码""""""支持的元素定位策略集合"""ID = "id"XPATH =
"xpath"LINK_TEXT = "link text"PARTIAL_LINK_TEXT =
"partial link text"NAME = "name"TAG_NAME = "tag
name"CLASS_NAME = "class name"CSS_SELECTOR = "css
selector"
**By** **包支持的定位器分类(****8** **种):**

ID 、XPATH 、LINK_TEXT 、PARTIAL_LINK_TEXT 、NAME 、TAG_NAME 、CLASS_NAME 、CSS_SELECTOR


二、定位页面元素方法用法汇总

(1)2.0 及以下低版本 selenium :By 定位页面元素方法用法(可忽略)

#从 selenium.webdriver.common.by 导入 By 包进行元素定位

from selenium.webdriver.common.by import By
# 先使用 find_element 和 find_elements 方法,再结合 By 类来指明定位分类来实现页面元素定位

# 写法:find_element(By.*,"变量值") ;find_element(By.*,"变量值")

# 示例:使用 tag name 定位单个元素
find_element(By.TAG_NAME,"tag name 值")

# 示例:使用 tag name 定位多个元素
find_elements(By.TAG_NAME,"tag name 值")

(2)3.0 ~ 3.9 版本 selenium:By 定位页面元素方法用法

【Python】selenium 基础使用:3.0 ~ 3.9 版本页面元素定位方法汇总

 1 #几种定位方式:
 2 #Autotest.py
 3 from selenium import webdriver
 4 from selenium.webdriver.common.by import By
 5 import time
 6 
 7 dr = webdriver.Chrome()
 8 dr.get("http://www.baidu.com")
 9 
10 #通过各种元素:name,id,class,tag,text
11 
12 dr.find_element_by_name('wd').send_keys('apple')              #name
13 dr.find_element_by_id('su').click                                     #id
14 dr.find_element_by_class_name('s_ipt').send_keys('apple')   #class
15 dr.find_element_by_tag_name('')                #tag    
16 dr.find_element_by_link_text('新闻').click()                #通过文字链接打开下级页面
17 dr.find_element_by_partial_link_text('一段很长的').click()#通过长文字的一部分定位元素
18 
19 
20 #从一组相同标签的元素里找到目标元素
21 inputs = dr.find_elements_by_tag_name('input')
22 for i in inputs:
23     if i.get_attribute('name') == "wd":
24         i.send_keys('apple')    
25 dr.find_element_by_id('su').click()
26 
27  
28 #xpath定位元素:
29 dr.find_element_by_xpath("//input[@id='kw']").send_keys("天气")     #xpath定位元素:input下面: id='kw'
30 dr.find_element_by_xpath("//*[@id='kw']").send_keys("天气")              #xpath定位元素:*=查找所有元素: id='kw'
31 
32  #父标签form,id='form' 下面第1个标签/span下面第1个/input(如果要定位的span是第二个则为/span[2])
33 dr.find_element_by_xpath("//form[@id='form']/span[1]/input[1]").send_keys("天气")   #这里【1】可以省略,只是为了理解
34 
35 dr.find_element_by_xpath("/html/body/div/div/div/div/div/form/span/input").send_keys('天气')   #xpath: 绝对路径定位元素
36 
37 #css选择器定位:
38 dr.find_element_by_css_selector('.s_ipt').send_keys("天气")      #.class
39 dr.find_element_by_css_selector('#kw').send_keys("天气")       #ID
40 dr.find_element_by_css_selector('input[maxlength="255"]').send_keys("天气")
41 
42 #下面这个意思是css定位:标签form(.表示class)class='fm' 下面的子标签span 的 子标签 input的 id='kw'(#表示id)
43 dr.find_element_by_css_selector('form.fm > span > input#kw').send_keys("天气")
44 
45 
46 #By类定位,需要引入By类
47 #dr.find_element(By.ID,'kw').send_keys('天气')
48 #dr.find_element(By.NAME,'wd').send_keys('天气')
49 #dr.find_element(By.CLASS_NAME,'s_ipt').send_keys('天气')
50 #dr.find_element(By.TAG_NAME,'input').send_keys('天气')      #这里定位不到,需要类似上面的for循环
51 #dr.find_element(By.LINK_TEXT,u'新闻').click()
52 #dr.find_element(By.PARTIAL_LINK_TEXT,u'新').click()
53 #dr.find_element(By.XPATH,"//*[@class='s_ipt']").send_keys('天气')
54 #dr.find_element(By.CSS_SELECTOR,'form.fm > span > input#kw').send_keys('天气')
55 
56 time.sleep(3)
57 dr.quit()

(3)4.0 ~ 4.9 版本 selenium:By 定位页面元素方法用法

selenium 4.0 ~ 4.9 写法共计 4 种,既能兼容 selenium 3.0 ~ 3.9 旧版写法,
也多出 2 种新版写法,注意:selenium 4 只能在 Python 3.7 及其以上版本使用。
""" selenium 4 旧版写法"""

""" 从 selenium 导入 webdriver 包进行元素定位"""

from selenium import webdriver

"""
注意:* 需小写字母,且用于括号外有连接符,用于括号内无连接符:

写法 1 :find_element_by_*("变量值") ;find_elements_by_*("变量值")

写法 2 :find_element(by = "*", value ="变量值") ;
		find_elements(by= "*", value = "变量值")

""" 

# 示例:使用 tag name 定位单个元素
find_element_by_tag_name("tag name 值")
find_element(by = "tag name", value = "tag name 值")

# 示例:使用 tag name 定位多个元素
find_elements_by_tag_name("tag name 值")
find_elements(by = "tag name", value ="tag name 值")
""" selenium 4 新版写法"""

""" 从 selenium 导入 webdriver 的 By 包进行元素定位"""

from selenium import webdriver

from selenium.webdriver.common.by import By

"""
注意:* 需大写字母且有连接符:

写法 1 :find_element(By.*,"变量值") ;find_elements(By.*,"变量值")

写法 2 :find_element(by = By.*, value = "变量值") ;
		 find_elements(by = By.*, value ="变量值")

"""

# 示例:使用 tag name 定位单个元素
find_element(By.TAG_NAME,"sb_form_q")
find_element(by = By.TAG_NAME, value ="sb_form_q")

# 示例:使用 tag name 定位多个元素
find_elements(By.TAG_NAME,"sb_form_q")
find_elements(by = By.TAG_NAME, value ="sb_form_q")

在这里插入图片描述


(4)页面元素定位方法汇总

定位方法定位方法说明定位单个元素写法(4 种)定位多个元素写法(4 种)
id使用 id 定位from selenium import webdriver
find_element_by_id("id 值")
find_element(by = "id", value = "id 值")
无:因为 id 为唯一值,所以不能定位多个
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.ID,"id 值")
find_element(by = By.ID, value = "id 值")
xpath使用 XPath 定位from selenium import webdriver
find_element_by_xpath("XPath 定位表达式")
find_element(by = "xpath", value = "XPath 定位表达式")
from selenium import webdriver
find_elements_by_xpath("XPath 定位表达式")
find_elements(by = "xpath", value = "XPath 定位表达式")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.XPATH,"XPath 定位表达式")
find_element(by = By.XPATH, value = "XPath 定位表达式")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.XPATH,"XPath 定位表达式")
find_elements(by = By.XPATH, value = "XPath 定位表达式")
link text使用链接的全部文字内容定位from selenium import webdriver
find_element_by_link_text("链接的全部文字内容")
find_element(by = "link text", value = "链接的全部文字内容")
from selenium import webdriver
find_elements_by_link_text("链接的全部文字内容")
find_elements(by = "link text", value = "链接的全部文字内容")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.LINK_TEXT,"链接的全部文字内容")
find_element(by = By.LINK_TEXT, value = "链接的全部文字内容")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.LINK_TEXT,"链接的全部文字内容")
find_elements(by = By.LINK_TEXT, value = "链接的全部文字内容")
partial link text使用链接的部分文字内容定位from selenium import webdriver
find_element_by_partial_link_text("链接的部分文字内容")
find_element(by = "partial link text", value = "链接的部分文字内容")
from selenium import webdriver
find_elements_by_partial_link_text("链接的部分文字内容")
find_elements(by = "partial link text", value = "链接的部分文字内容")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.PARTIAL_LINK_TEXT,"链接的部分文字内容")
find_element(by = By.PARTIAL_LINK_TEXTK_TEXT, value = "链接的部分文字内容")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.PARTIAL_LINK_TEXT,"链接的部分文字内容")
find_elements(by = By.PARTIAL_LINK_TEXTK_TEXT, value = "链接的部分文字内容")
name使用 name 定位from selenium import webdriver
find_element_by_name("name 值")
find_element(by = "name", value = "name 值")
from selenium import webdriver
find_elements_by_name("name 值")
find_elements(by = "name", value = "name 值")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.NAME,"name 值")
find_element(by = By.NAME, value = "name 值")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.NAME,"name 值")
find_elements(by = By.NAME, value = "name 值")
tag name使用标签名称定位from selenium import webdriver
find_element_by_tag_name("页面中的 HTML 标签名称")
find_element(by = "tag name", value = "页面中的 HTML 标签名称")
from selenium import webdriver
find_elements_by_tag_name("页面中的 HTML 标签名称")
find_elements(by = "tag name", value = "页面中的 HTML 标签名称")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.TAG_NAME,"页面中的 HTML 标签名称")
find_element(by = By.TAG_NAMEME, value = "页面中的 HTML 标签名称")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.TAG_NAME,"页面中的 HTML 标签名称")
find_elements(by = By.TAG_NAMEME, value = "页面中的 HTML 标签名称")
class name使用 class name 定位from selenium import webdriver
find_element_by_class_name("页面元素的 Class 属性值")
find_element(by = "class name", value = "页面元素的 Class 属性值")
from selenium import webdriver
find_elements_by_class_name("页面元素的 Class 属性值")
find_elements(by = "class name", value = "页面元素的 Class 属性值")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.CLASS_NAME,"页面元素的 Class 属性值")
find_element(by = By.CLASS_NAME, value = "页面元素的 Class 属性值")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.CLASS_NAME,"页面元素的 Class 属性值")
find_elements(by = By.CLASS_NAME, value = "页面元素的 Class 属性值")
css selector使用 CSS 方式定位from selenium import webdriver
find_element_by_css_selector("CSS 定位表达式")
find_element(by = "css selector", value = "CSS 定位表达式")
from selenium import webdriver
find_elements_by_css_selector("CSS 定位表达式")
find_elements(by = "css selector", value = "CSS 定位表达式")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.CSS_SELECTOR,"CSS 定位表达式")
find_element(by = By.CSS_SELECTOR, value = "CSS 定位表达式")
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.CSS_SELECTOR,"CSS 定位表达式")
find_elements(by = By.CSS_SELECTOR, value = "CSS 定位表达式")
>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值