目录
一、定位页面元素方法源码说明
(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 Documentationhttps://www.selenium.dev/selenium/docs/api/py/api.html#webdriver-common
(2)selenium.webdriver.common.by
selenium.webdriver.common.byhttps://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.byhttps://www.selenium.dev/selenium/docs/api/py/_modules/selenium/webdriver/common/by.html#By
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.0Unless 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 版本页面元素定位方法汇总https://gusanshang.blog.csdn.net/article/details/111936507
(3)4.0 ~ 4.9 版本 selenium:By 定位页面元素方法用法
selenium 4.0 ~ 4.9 写法共计 4 种,既能兼容 selenium 3.0 ~ 3.9 旧版写法,也多出 2 种新版写法,注意:selenium 4 只能在 Python 3.7 及其以上版本使用。文章来源:https://www.toymoban.com/news/detail-411871.html
""" 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")
文章来源地址https://www.toymoban.com/news/detail-411871.html
(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 定位表达式") |
到了这里,关于【Python】Python 模块用法:selenium 4 版本页面元素定位方法汇总的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!