selenium 元素 是否存在 是否选中 是否启用状态
# 判断元素是否...... # 导包 from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep # 定义类 class GetEle: def __init__(self,driver): self.driver=driver def get_e(self,loc): e=driver.find_element(By.CSS_SELECTOR,loc) print(e.text) def is_find(self,loc): flag=driver.find_element(By.CSS_SELECTOR,loc).is_displayed() print(fr"存在吗?:{flag}") def is_selec(self,loc): driver.find_element(By.CSS_SELECTOR,loc).click() sleep(3) flag=driver.find_element(By.CSS_SELECTOR,loc).is_selected() print(fr"被选中l吗?:{flag}") def is_en(self,loc): flag=driver.find_element(By.CSS_SELECTOR,loc).is_enabled() print(fr"启用状态码?:{flag}") # 调用 if __name__ == '__main__': # 调用driver driver=webdriver.Chrome() # url url=r"D:\XXXX\myself\1.html" driver.get(url) # 实例化类 web_e=GetEle(driver) # 调用方法1 test_loc='[href="//www.runoob.com/web/web-buildingprimer.html"]>h4' # 调用方法2 test_is_find_loc='p[hidden]' # 调用方法3 loc3='#choose' # 调用方法4 loc4='[type="button"]' web_e.is_en(loc4) # 退出 driver.quit()
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DAY4</title> </head> <body> <p hidden>段落1</p><br> 单选:<input type="radio" id="choose"> <button type="button" disabled>按钮2</button> </body> </html>