selenium:class屬性內(nèi)帶有空格的定位坑

  • 前言
    • 由于web頁面元素class屬性值帶有空格,導(dǎo)致直接使用class屬性值元素定位失敗
    • 如: class="inputstyle password" ,直接使用定位:driver.find_element_by_class_name("inputstyle password").send_keys("1111")
    • **html classname值描述: **規(guī)定元素的類的名稱。如需為一個(gè)元素規(guī)定多個(gè)類,用空格分隔類名。
    • W3cschool 對于class屬性介紹
  • 報(bào)錯(cuò)日志
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值包含多個(gè)類,可使用任意單個(gè)唯一類型來定位,如: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")
    
    # 單個(gè)唯一屬性
    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")

# 由于當(dāng)前PC已登錄QQ,需從快速登錄切換至賬號密碼登錄方式
# 若無可注釋
driver.find_element_by_class_name("switch_btn").click()

# 報(bào)錯(cuò)用法
# driver.find_element_by_class_name("inputstyle password").send_keys("1111")

# inputstyle 為賬號輸入框class屬性值,即會(huì)輸入至賬號輸入框
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定位報(bào)錯(cuò)、web自動(dòng)化、安卓自動(dòng)化、class定位、控件定位

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容