Selenium4自动化测试6--控件获取数据--下拉框级联选择、checkbox选择、时间选择器

4-下拉框级联选择

复制代码
import time
from selenium.webdriver.support.select import Select
#pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By

# 定义一个driver的变量,用来接收实例化后的浏览器
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)
# 使用get方法,访问网址
driver.get("https://www.iviewui.com/view-ui-plus/component/form/cascader")
driver.find_element(By.XPATH,'//input[@class="ivu-input ivu-input-default"]').click()
driver.find_element(By.XPATH,'//li[contains(text(),"北京")]').click()
driver.find_element(By.XPATH,'//li[contains(text(),"王府井")]').click()
time.sleep(3)
driver.quit()

5-checkbox选择

复制代码
import time

#pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
# 定义一个driver的变量,用来接收实例化后的浏览器
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)
# 使用get方法,访问网址
driver.get('https://www.iviewui.com/view-ui-plus/component/form/checkbox')
#窗口最大化
driver.maximize_window()
driver.find_element(By.XPATH,'//span[text()="香蕉"]').click()
time.sleep(3)
driver.find_element(By.XPATH,'//span[text()="苹果"]').click()
time.sleep(3)
driver.find_element(By.XPATH,'//span[text()="西瓜"]').click()
time.sleep(3)

#同级哥哥写法
# driver.find_element(By.XPATH,'//span[text()="香蕉"]/preceding-sibling::span/input').click()
# time.sleep(3)
# driver.find_element(By.XPATH,'//span[text()="苹果"]/preceding-sibling::span/input').click()
# time.sleep(3)
# driver.find_element(By.XPATH,'//span[text()="西瓜"]/preceding-sibling::span/input').click()
# time.sleep(3)


time.sleep(3)
driver.quit()

6-时间选择器

复制代码
import time
from selenium.webdriver.support.select import Select
#pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By

# 定义一个driver的变量,用来接收实例化后的浏览器
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)
# 使用get方法,访问网址
driver.get("https://www.iviewui.com/view-ui-plus/component/form/date-picker")
driver.find_element(By.XPATH,'//input[@class="ivu-input ivu-input-default ivu-input-with-suffix"]').send_keys("2023-11-21")
driver.find_elements(By.XPATH,'//input[@class="ivu-input ivu-input-default ivu-input-with-suffix"]')[1].send_keys("2023-11-10 2023-11-21")
time.sleep(3)
driver.quit()

时间选择器这里特别说明一下,有的时间选择器直接赋值不行,空间是readonly这时就要用js修改控件的属性,不让他readonly 如下:

复制代码
#执行js 去掉日期控件的readonly
btime = '//*[@id="btime"]'
js_b = "document.getElementById('btime').removeAttribute('readonly')"
driver.execute_script(js_b)
相关推荐
计算机源码社12 分钟前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
汤兰月18 分钟前
Python中的观察者模式:从基础到实战
开发语言·python·观察者模式
西柚与蓝莓2 小时前
【开源开放体系总结】
python
belldeep5 小时前
python:reportlab 将多个图片合并成一个PDF文件
python·pdf·reportlab
FreakStudio8 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
丶21368 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
_.Switch9 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一个闪现必杀技9 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
小鹿( ﹡ˆoˆ﹡ )10 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温10 小时前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug