python+selenium小結9:判斷單選按鈕是否被選中

 

is_selected() # 判斷按鈕是否被選中,選中返回True,沒有選中返回false

 

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
from selenium import webdriver
import time

options = webdriver.ChromeOptions()
prefs = {
    'profile.default_content_setting_values':
        {
            'notifications': 2
        }
}
options.add_experimental_option('prefs', prefs)  # 關掉瀏覽器左上角的通知提示
options.add_argument("disable-infobars")  # 關閉'chrome正受到自動測試軟件的控制'提示
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://wenku.baidu.com/")  # 百度文庫
radios = driver.find_elements_by_xpath("//*/input[@type='radio']")  # 找到頁面的單選按鈕
count = 0
for radio in radios:
    is_selected = radio.is_selected()  # 判斷按鈕是否被選中,選中返回True,沒有選中返回false
    if is_selected:  # 返回True時,就執行下面的語句
        count += 1
        print("被選中", " ", count)
    else:
        print("沒有被選中", is_selected)
time.sleep(5)
driver.quit()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章