import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("file:///C:/Users/S3214/Downloads/pagetest/%E6%B3%A8%E5%86%8CA.html")
selected = driver.find_element(By.CSS_SELECTOR,'#lyA').is_selected()print(selected)# 法1 selected返回值为Ture,满足条件可以直接用selected代替Ture# if为Ture(真),打印1;否则(为假),打印2if selected:print("【旅游】复选框被选中")else:print("【旅游】复选框没被选中")
time.sleep(3)
driver.quit()
效果图
/*
C:\Programs\Python\Python38\python.exe C:/pythonProject/ke.py
True
【旅游】复选框被选中
Process finished with exit code 0*/
法2【不严谨】
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("file:///C:/Users/S3214/Downloads/pagetest/%E6%B3%A8%E5%86%8CA.html")
selected = driver.find_element(By.CSS_SELECTOR,'#lyA').is_selected()print(selected)if(selected ==True):print("【旅游】复选框被选中")else:print("【旅游】复选框没被选中")
time.sleep(3)
driver.quit()
C:\Programs\Python\Python38\python.exe C:/pythonProject/ke.py
True
【旅游】复选框被选中
Process finished with exit code 0
判断元素是否可可见
is_displayed()
方法返回一个布尔值,若可见则返回: True 。若不可见则返回: False 。
Code
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("file:///C:/Users/S3214/Downloads/pagetest/%E6%B3%A8%E5%86%8CA.html")# span标签是否可见
display=driver.find_element(By.CSS_SELECTOR,'span').is_displayed()if(display ==True):print("span标签可见")else:print("span标签不可见")
time.sleep(3)
driver.quit()