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

2021-02-21  本文已影响0人  R_zb
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)
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定位、控件定位

上一篇下一篇

猜你喜欢

热点阅读