UI自动化写脚本【自用】
- 定位方式
-
-
- 常用操作
-
定位方式 1、id
2、name
3、class_name
4、tag_name
5、link_text
6、partial_link_text
7、XPath
8、CSS
通过属性定位元素: find_element_by_xpath(“//标签名[@属性=‘属性值’]”)
id属性
find_element_by_xpath("//input[@id=‘’]")
class属性
find_element_by_xpath("//input[@class=‘’]")
name属性
find__element_by_xpath("//input[@name=‘’]")
常用操作
(1)点击操作click():
self.driver.find_element_by_xpath(xpath).click()self.driver.find_element_by_id(xpath).click()self.driver.find_element_by_class_name(xpath).click()self.driver.find_element_by_css_selector(xpath).click()self.driver.find_elements_by_xpath(xpath)[number].click()
self.driver.find_elements_by_css_selector(xpath)[number].click()

文章图片

文章图片
(2)输入内容send_keys():
self.driver.find_element_by_xpath(xpath).send_keys(keys)self.driver.find_element_by_id(xpath).send_keys(keys)self.driver.find_element_by_class_name(xpath).send_keys(keys)self.driver.find_element_by_css_selector(xpath).send_keys(keys)self.driver.find_elements_by_xpath(xpath)[number].send_keys(keys)
self.driver.find_elements_by_css_selector(xpath)[number].send_keys(keys)

文章图片
(3)清除clears():
self.driver.find_element_by_xpath(xpath).clear()self.driver.find_element_by_id(xpath).clear()self.driver.find_element_by_class_name(xpath).clear()self.driver.find_element_by_css_selector(xpath).clear()self.driver.find_elements_by_xpath(xpath)[number].clear()
self.driver.find_elements_by_css_selector(xpath)[number].clear()
【测试|UI自动化】

文章图片

文章图片
(4)下拉选择drop():
self.driver.find_element_by_xpath(xpath).click()
sleep(1)
self.driver.find_element_by_xpath(xpath1).click()self.driver.find_element_by_id(xpath).click()
sleep(1)
self.driver.find_element_by_id(xpath1).click()self.driver.find_element_by_class_name(xpath).click()
sleep(1)
self.driver.find_element_by_class_name(xpath1).click()self.driver.find_element_by_css_selector(xpath).click()
sleep(1)
self.driver.find_element_by_css_selector(xpath1).click()

文章图片

文章图片
1.下拉选择
self.driver.find_element_by_xpath(‘//[@x-placement=“bottom-start”]/div/div/ul/li[1]‘).click()
2.上拉选择
self.driver.find_element_by_xpath(’//[@x-placement=“top-start”]/div/div/ul/li[1]’).click()
(5)参数对比:
""参数对比check"""
text_data = https://www.it610.com/article/self.driver.find_element_by_xpath(xpath).text
print(text_data)
assert text == text_data""参数对比check_error"""
text_data = https://www.it610.com/article/self.driver.find_element_by_xpath(xpath).text
print(text_data)
assert text != text_data
(6)悬浮按钮操作choice_xf):
ac = ActionChains(self.driver)# 获取实例化ActionChains类
act = self.driver.find_element_by_xpath(xpath)
ac.move_to_element(act).perform()
sleep(1)
self.driver.find_element_by_xpath(xpath1).click()
if mold == 'css':
ac = ActionChains(self.driver)# 获取实例化ActionChains类
act = self.driver.find_element_by_css_selector(xpath)
ac.move_to_element(act).perform()
sleep(1)
self.driver.find_element_by_css_selector(xpath1).click()choices_xf():
ac = ActionChains(self.driver)# 获取实例化ActionChains类
act = self.driver.find_element_by_xpath(xpath)
ac.move_to_element(act).perform()
sleep(1)
self.driver.find_elements_by_xpath(xpath1)[number].click()
(7)元素按tab键:
self.driver.find_element_by_xpath(xpath).send_keys(Keys.TAB)
if mold == 'css':
self.driver.find_element_by_css_selector(xpath).send_keys(Keys.TAB)
(8)按住元素进行拖动:
a = self.driver.find_element_by_xpath(xpath)
ac = ActionChains(self.driver)
ac.drag_and_drop_by_offset(a, x, y).perform()
(9)获取当天日期:
self.driver.find_element_by_xpath("//input[@placeholder='请输入 名称']").send_keys(str(datetime.date.today()))self.driver.find_elements_by_xpath("//input[@placeholder='请输入 名称']")[1].send_keys(str(datetime.date.today()))
(10)获取明天日期:
tomorrow = str(datetime.date.today() + datetime.timedelta(days=1))
self.driver.find_element_by_xpath("//input[@placeholder='请输入 名称']").send_keys(tomorrow)
推荐阅读
- webpack|Webpack(一)
- javascript|this相关问题
- 前端总结|疫情期间我做了这些,成功拿到30K前端开发职位!
- web前端|Web前端培训分享(Web前端三大主流框架对比)
- 布局之悬浮显示更多文本并增加箭头指示效果
- python 修改python插件包的默认安装路径
- DGIOT基本功能介绍——组态编辑配置
- 机器学习|【进阶版】机器学习之贝叶斯分类器细节回顾及原理完善(10)
- 机器学习|【进阶版】机器学习之线性模型介绍及过拟合欠拟合解决方法岭回归、loss回归、elasticnet回归(05)