文章目录
寄语
让有意义的事情变的有意思,让有意思的事情变的有意义。
一、自动化测试是什么?
自动化测试是指通过自动化测试工具或其他手段,按照测试人员的测试计划进行自动测试,目的是减轻手工测试的工作量。
二、操作
1.环境搭建
1.为python安装selenium模块:pip install selenium
2.下载相应浏览器驱动:Chromedriver;Firefoxdriver
2.selenium知识点汇总
一、selenium之元素定位
正常情况下优先级:id>name>CSS selector>xpath
优先选择CSS selector的原因
1.css配合html工作,xpath配合xml工作
2.相对xpath,更简洁
3.前端开发主要使用css
除了find_element_by_xxx(),find_element()函数也可以定位
需要导入By即from selenium.webdrive.common.by import By.
from selenium import webdriver
from time import sleep
"""
测试demon是http://sahitest.com/demo/
"""
class Dwelement(object):
#初始化
def __init__(self):
#实例化浏览器
self.drive=webdriver.Chrome()
#URl
self.drive.get('http://sahitest.com/demo/')
#全屏
self.drive.maximize_window()
#find_element_by_id
def Dw_by_id(self):
self.drive.find_element_by_link_text('Link Test').click()
sleep(1)
self.drive.find_element_by_id('linkById').click()
#返回
self.drive.back()
#返回主界面
self.drive.back()
#find_element_by_name
def Dw_by_name(self):
self.drive.find_element_by_link_text('Form Test').click()
#弹框
al=self.drive.switch_to.alert
sleep(1)
al.accept()
self.drive.find_element_by_name('t1').send_keys('hello selenium')
sleep(1)
self.drive.back()
#find_element_by_css
def Dw_by_css(self):
self.drive.find_element_by_link_text('Form Test').click()
#处理弹框
al=self.drive.switch_to.alert
al.accept()
self.drive.find_element_by_css_selector('body > form > table > tbody > tr:nth-child(3)\
> td:nth-child(2) > input[type=text]').send_keys('hello')
#find_element_by_xpth
def Dw_by_xpth(self):
self.drive.find_element_by_link_text('Form Test').click()
#处理弹框
al=self.drive.switch_to.alert
sleep(1)
al.accept()#/html/body/form/table/tbody/tr[3]/td[2]/input
# self.drive.find_element_by_xpath('/html/body/form/table/tbody/tr[3]/td[2]/input').send_keys('hello')
self.drive.find_element_by_xpath('.//tr[3]/td[2]/input')

本文介绍了自动化测试的基础概念,详细讲解了如何使用Python的selenium库进行环境搭建和元素定位,包括ID、name、CSS选择器、XPath的应用,以及浏览器控制、iframe切换、多标签页操作、鼠标键盘事件和额外功能如下拉框、警告框处理等。
最低0.47元/天 解锁文章
5682

被折叠的 条评论
为什么被折叠?



