013、radio 和 checkbox 操作
一、radio 和 checkbox 操作
1. radio 单选
2. checkbox 复选
示例代码如下:
# -*- coding:utf-8 -*-
# Author: Sky
# Email: 2780619724@qq.com
# Time: 2021/8/18 0:32
# Project: day01
# Module: study_15.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://49.235.92.12:8200/users/login/")
time.sleep(3)
driver.find_element_by_id("username").send_keys("112233456@qq.com")
driver.find_element_by_id("password_l").send_keys("123456")
driver.find_element_by_id("jsLoginBtn").click()
time.sleep(2)
# 鼠标悬停的操作
mouse = driver.find_element_by_class_name("personal")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
# 点进入个人中心
driver.find_element_by_class_name("personcenter").click()
time.sleep(2)
# radio 相关操作
element_nv = driver.find_element_by_xpath('//*[@id="jsEditUserForm"]/ul/li[4]/label[2]/input')
# 1.判断未点击之前元素的状态
status_1 = element_nv.is_selected() # 判断元素是否被选中 False/True
print("未点击之前元素的状态:%s" % status_1)
# 点击元素
element_nv.click()
status_2 = element_nv.is_selected() # 判断元素是否被选中 False/True
print("点击之后元素的状态:%s" % status_2)
# check_box
driver.find_element_by_xpath('//*[@value="cypress"]').click()
# 状态判断is_selected()
cypress = driver.find_element_by_xpath('//*[@value="cypress"]')
print(f'未点击之前的状态:{cypress.is_selected()}')
if not cypress.is_selected():
cypress.click()
print(f'点击之后的状态:{cypress.is_selected()}')
# checkbox 全部勾选
all_checkboxs = driver.find_elements_by_name("fancy")
print(all_checkboxs)
# 循环,先判断后操作
for i in all_checkboxs:
if not i.is_selected():
i.click()
print("打印checkbox的状态:%s" % i.is_selected())
driver.quit()