selenium的UI自动化框架入门

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

环境准备

python、pycharme、chromedriver

google下载的官网地址

https://google.cn/chrome/

chromedriver

chromedriver的下载

https://chromedriver.storage.googleapis.com/index.html

chromedriver配置环境变量

C:\Users\Administrator\.cache\selenium\chromedriver\win64\127.0.6533.88

安装selenium插件

通过加载cookie等缓存信息绕过登录

chrome://version/

代码中加载cookie信息

选择器

ID、NAME、XPATH、CLASS_NAME、LINK_TEXT、TAG_NAME、CSS_SELECTOR、XPATH

By.CSS_SELECTOR选择器

#coding:utf-8
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 实例化chrome配置
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('detach', True)  #设置执行完毕后浏览器不自动关闭

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.baidu.com/")
#根据class定位时使用的
#driver.get("https://www.bilibili.com/")
driver.maximize_window()
#根据id定位
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("selenium")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
#根据class属性值进行定位
# driver.find_element(By.CSS_SELECTOR,".nav-search-input").send_keys("你好")
#根据name属性值定位
#driver.find_element(By.CSS_SELECTOR,"[name='wd']").send_keys("selenium")
#根据标签属性定位
#不通 driver.find_element(By.CSS_SELECTOR,'a[href="http://image.baidu.com"]').click()
#定位子元素
#driver.find_element(By.CSS_SELECTOR,'div#s-top-left>a').click()
#获取集合根据下标定位
#driver.find_elements(By.CSS_SELECTOR,'div#s-top-left>a')[3].click()
driver.find_element(By.CSS_SELECTOR,'div#s-top-left>a:first-child').click()

保持登录状态的代码执行

import time
from selenium import webdriver
import os,platform

cookies_HWWAFSESID = {
    "name":"HWWAFSESID",
    "value":"faa7673bf6d39c6dca"
}
cookies_HWWAFSESTIME = {
    "name":"HWWAFSESTIME",
    "value":"1722990313412"
}
if platform.system() =="Windows":
    os.system("taskkill -im chrome* -f")  # 把chrome开头的进程都杀掉
else:
    os.system("killall -9 chrome*")
user_data = r"C:\Users\Administrator\AppData\Local\Google\Chrome\User Data"
profile_directory = rf'--user-data-dir={user_data}'
# 实例化chrome配置
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('detach', True)
chrome_options.add_argument(profile_directory)
driver = webdriver.Chrome(options=chrome_options)

driver.get("https://www.cherygpt.com/home")
# time.sleep(5)
# driver.add_cookie(cookies_HWWAFSESID)
# driver.add_cookie(cookies_HWWAFSESTIME)
# driver.refresh()
# time.sleep(3)
# driver.get("https://www.cherygpt.com/home")
# print("刷新已经执行!")

By.XPATH的元素定位

import os,platform
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 实例化chrome配置
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('detach', True)  #设置执行完毕后浏览器不自动关闭

driver = webdriver.Chrome(options=chrome_options)

#driver.get("https://www.baidu.com")

#打开element网页
#driver.get("https://element.eleme.cn/#/zh-CN/component/cascader")
#单选框radio的定位
#driver.get("https://iviewui.com/view-ui-plus/component/form/radio")
#多选checkbox
#driver.get("https://iviewui.com/view-ui-plus/component/form/checkbox")
#select定位
# driver.get("https://sahitest.com/demo/selectTest.htm")
#级联选择器
# driver.get("https://iviewui.com/view-ui-plus/component/form/cascader")
#alert弹框没有调试成功
#driver.get("https://sahitest.com/demo/alertTest.htm")
#upload上传文件
driver.get("https://sahitest.com/demo/php/fileUpload.htm")
driver.maximize_window()
#单选框radio定位
# driver.find_elements(By.XPATH,'//input[@class="ivu-radio-input" and @type="radio"]')[1].click()
# time.sleep(2)
# driver.find_elements(By.XPATH,'//input[@class="ivu-radio-input" and @type="radio"]')[2].click()
# time.sleep(2)
# driver.find_elements(By.XPATH,'//input[@class="ivu-radio-input" and @type="radio"]')[3].click()
#Checkbox的多选框的选择
# driver.find_element(By.XPATH,'//span[text()="香蕉"]/preceding-sibling::span/input').click()
# time.sleep(2)
# driver.find_element(By.XPATH,'//span[text()="苹果"]/preceding-sibling::span/input').click()
# time.sleep(2)
# driver.find_element(By.XPATH,'//span[text()="西瓜"]/preceding-sibling::span/input').click()
#select选择框
# select = Select(driver.find_element(By.ID,"s1"))
# select.select_by_index(1)
#级联选择
# 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()
#获取弹框
# driver.find_element(By.NAME,"b1").click()
# #使用alert.text获取弹框的文字
# print(driver.switch_to.alert.text)
# #点击确定
# driver.switch_to.alert.accept()
#上传文件
upload = driver.find_element(By.ID,'file')
upload.send_keys(r"D:/PythonProject/PythonProject/UI自动化/file/20240510-095544.jpg")
driver.find_element(By.NAME,'submit').click()


#绝对定位
#driver.find_element(By.XPATH, '/html/body/div/div/div[3]/a').click()
#相对定位
#根据ID定位
#driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys("selenium")
#根据class定位

#根据多个属性组合定位
#driver.find_element(By.XPATH,'//input[@id="kw" and @class="s_ipt" and @autocomplete="off"]').send_keys("selenium")
#多组数据使用下标定位
#driver.find_element(By.XPATH,'//div[@id="s-top-left"]/a[4]').click()

#根据文本进行定位
#driver.find_element(By.XPATH, '//span[text()="黑神话发售 有公司宣布给员工放假"]').click()

#根据同级弟弟元素定位
#driver.find_element(By.XPATH,'//span[text()="默认 click 触发子菜单"]/following-sibling::div[1]//div//input').click()

#根据同级哥哥定位
#driver.find_element(By.XPATH, '//a[contains(text(),"贴吧") and @class="mnav c-font-normal c-color-t"]/preceding-sibling::a').click()

iframe

#coding:utf-8
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 实例化chrome配置
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('detach', True)  #设置执行完毕后浏览器不自动关闭

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://sahitest.com/demo/iframesTest.htm")
driver.maximize_window()

driver.find_element(By.ID,"checkRecord").clear()
driver.find_element(By.ID,"checkRecord").send_keys("666")
time.sleep(3)
#用下标
driver.switch_to.frame(1)
driver.find_element(By.ID,'open-self').click()
# time.sleep()

组件的练习

https://iviewui.com/view-ui-plus/component/form/radio

UI自动化的PO思想模式

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值