selenium:class属性内带有空格的定位坑

本文介绍了在使用selenium进行web自动化测试时,遇到元素class属性值带有空格导致定位失败的问题。通过分析HTML的class属性和错误日志,提出了两种解决方案:一是使用class值中的单个唯一类型进行定位;二是利用css_selector方法进行更精确的定位。文章还提及了Appium和安卓自动化测试的相关内容。
  • 前言
    • 由于web页面元素class属性值带有空格,导致直接使用class属性值元素定位失败
    • 如: class=“inputstyle password” ,直接使用定位:driver.find_element_by_class_name(“inputstyle password”).send_keys(“1111”)
    • **html classname值描述: **规定元素的类的名称。如需为一个元素规定多个类,用空格分隔类名。
    • W3cschool 对于class属性介绍
  • 报错日志
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".inputstyle password"}
  (Session info: chrome=81.0.4044.138)
  • 示例:QQ邮箱 - 账号/密码输入框

    • 账号输入框:class = inputstyle

    • 密码输入框:class = inputstyle password
      示例

  • 解决办法

    • 方法一:driver.find_element_by_class_name方式定位
      • 控件class值包含多个类,可使用任意单个唯一类型来定位,如:inputstyle、password
      • 但是由于账号输入框class属性值为inputstyle,需取唯一属性,故密码输入框只能使用password
    driver.find_element_by_class_name("password").send_keys("1111")
    
    • 方法2:driver.find_element_by_css_selector
    # class属性定位
    driver.find_element_by_css_selector("[class='inputstyle password']").send_keys("1111")
    
    # 各空格变"."
    driver.find_element_by_css_selector(".inputstyle.password").send_keys("1111")
    
    # 单个唯一属性
    driver.find_element_by_css_selector(".password").send_keys("5555")
    
  • 示例代码

from selenium import webdriver
import time


driver = webdriver.Chrome()
driver.get("https://mail.qq.com/")
driver.implicitly_wait(10)

driver.switch_to.frame("login_frame")

# 由于当前PC已登录QQ,需从快速登录切换至账号密码登录方式
# 若无可注释
driver.find_element_by_class_name("switch_btn").click()

# 报错用法
# driver.find_element_by_class_name("inputstyle password").send_keys("1111")

# inputstyle 为账号输入框class属性值,即会输入至账号输入框
driver.find_element_by_class_name("inputstyle").clear()
driver.find_element_by_class_name("inputstyle").send_keys("1111")
time.sleep(2)

# password 为密码输入框唯一属性值,即正常输入至密码输入框
driver.find_element_by_class_name("password").clear()
driver.find_element_by_class_name("password").send_keys("2222")
time.sleep(2)

# css_selector定位:取class属性定位,即正常输入至密码输入框
driver.find_element_by_css_selector("[class='inputstyle password']").clear()
driver.find_element_by_css_selector("[class='inputstyle password']").send_keys("3333")
time.sleep(2)

# css_selector定位:“.”替换空格,即正常输入至密码输入框
driver.find_element_by_css_selector(".inputstyle.password").clear()
driver.find_element_by_css_selector(".inputstyle.password").send_keys("4444")
time.sleep(2)

# css_selector定位:“.”替换空格并取唯一属性值,即正常输入至密码输入框
driver.find_element_by_css_selector(".password").clear()
driver.find_element_by_css_selector(".password").send_keys("5555")
time.sleep(2)

# css_selector定位:“.”替换空格,inputstyle为账号输入框唯一属性值,即正常输入至账号输入框
driver.find_element_by_css_selector(".inputstyle").clear()
driver.find_element_by_css_selector(".inputstyle").send_keys("5555")


Key words

Appium、selenium、class定位报错、web自动化、安卓自动化、class定位、控件定位

### 如何用 Selenium 定位页面上具有相同 class 名称的元素 在 Selenium 中,当需要定位多个具有相同 `class` 属性的元素时,可以使用 `find_elements` 方法来获取这些元素组成的列表。通过遍历此列表,可以选择特定索引位置上的元素并对其进行操作。 以下是实现这一功能的具体方式: #### 使用 `find_elements` 获取多个相同 Class 的元素 可以通过 `By.CLASS_NAME` 或其他更灵活的选择器(如 CSS Selector 和 XPath)来查找一组匹配的元素。例如,在引用中提到的方法[^2]展示了如何利用 `find_elements` 来处理这种情况。具体代码如下所示: ```python from selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Chrome() try: driver.get("https://www.youkuaiyun.com/") # 查找所有具有指定类名的元素 elements = driver.find_elements(By.CLASS_NAME, 'navigation-right') if len(elements) > 0: # 对第3个对象进行点击操作 (注意 Python 列表是从0开始计数) third_element = elements[2] third_element.click() finally: time.sleep(3) driver.quit() ``` 上述代码片段实现了以下逻辑: - 打开目标网页。 - 调用 `find_elements` 函数找到所有的 `.navigation-right` 类型元素,并存储在一个列表中。 - 针对该列表中的第三个元素执行点击动作。 需要注意的是,某些情况下直接复制带有空格的复合 `class` 可能引发异常 `InvalidSelectorException`[^3]。这是因为 Selenium 不支持直接针对多部分组成 (`multi-part`) 的 `class` 进行精确匹配。此时建议改用 **CSS Selectors** 或者 **XPath 表达式** 实现更加复杂的筛选条件。 #### 处理复杂情况:带空格或其他特殊字符的 Class Name 对于那些包含空格或者其它非法字符作为分隔符的情况,则推荐采用基于 CSS 的组合查询语句代替单一的 `CLASS_NAME` 参数形式。比如下面的例子演示了怎样绕过这种限制完成相似的任务: ```python elements_with_space_in_class = driver.find_elements( By.CSS_SELECTOR, ".pager-btn.pager-next" ) if elements_with_space_in_class: first_match = elements_with_space_in_class[0] first_match.click() ``` 在这里,`.pager-btn.pager-next` 是一种标准写法用于指示同时满足两个子类别约束的目标节点集合成员之一即可触发交互行为。 另外值得注意的一点在于 SVG 图形内部组件可能也需要特别对待才能成功识别出来[^4]。这通常涉及到调整默认路径解析策略以及引入额外命名空间声明等内容。 --- ### 总结 综上所述,要解决 Selenium定位多个相同 `class` 名称的问题,主要依赖于以下几个方面: 1. 应用 `find_elements()` 方法批量检索符合条件的所有 DOM 结构实例; 2. 当面对含有空白间隔或者其他不兼容语法结构的时候切换至更为强大的选择机制——即 CSS selectors/XPath expressions; 3. 特殊场景下还需考虑诸如 iframe 嵌套、动态加载数据延迟显现等因素的影响; 希望以上解答能够帮助到您!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值