天天看点

Python selenium webdriver GUI自动化测试Selenium webdriver常用操作

Selenium webdriver常用操作

1.安装pip install selenium

通过selenium库得到浏览器的驱动对象(需要下载驱动程序,不同的浏览器需要不同的 驱动对象,Firefox->geckodriver,Chrome->chromedriver)

2.得到驱动对象后,可以通过驱动对象:

Driver.get()-> 打开一个网站(需要完整的URL)

Driver.close()-> 仅仅关闭当前页面,如果他打开的仅一个窗口则退出浏览器

Driver.quit()-> 关闭驱动程序打开的所有页面,同时释放驱动对象

Os.system()/os.popen()->杀死整个驱动程序进程

Driver.maximize_window()->最大化浏览器窗口

Driver.set_window_size(x,y)-> 设置窗口的大小

Driver.back()->回退

Driver.forword()->前进

driver.refresh()->页面刷新

3.selenium常用操作:

(1)查找页面元素,并操作#

A.查找元素::Driver.find_element_by_method()

Method():

Id() -->一般来说,id属性在标签里面具有唯一性

Name()–>和id相同,一般都是标签的唯一属性

Class_name()–>相同类型的标签的属性

当class属性在页面存在多个时:可以用find_elements_by_class_name先找出所有的class属性并放入列表,再根据索引将他取出来进行操作

li = wd.find_elements_by_class_name(“el-input__inner”)

li[0].send_keys(“admin”)

li[1].send_keys(“CDDQ&1314!”)

Link_text()-->  <a>标签专用,超链接
Partial_link_text()--> <a>标签专用 ,超链接
Xpath()-->标签相对于<html>的树形结构
Css_selector()-->标签相对于css的树形结构
Tag_name()-->以标签名的方式定位
           

B.操作:

输入:send_keys(“ ”)
点击:click()
清空输入框:clear()
判断元素是否显示:is_displayed()
           

判断元素是否可使用:is_enabled()

(2)等待方式:

(1)隐式等待:driver.implicitly_wait( )
(2)显示等待:from selenium.webdriver.support.ui import WebDriverWait
Webdriverwait(driver,10,0.1).until(lambda driver:	driver .find_element_by_[method{‘xx’}]).send_keys()
(3)强制等待:time .sleep()
           

(3)焦点切换:

内嵌页面的方式:iframe->

进入内嵌页面:
	 driver.switch_to.frame(id/driver.find_element_by_[method()])
	 driver.find_element_by_[method].func()
	关闭内嵌页面:
          driver.switch_to.default_content()
           

警告框: alt = driver.swidth_to.alert.accept()

alt.accept()->确定
	alt.dismiss()->取消
	alt.text()->获取文本内容
           

(4)文件上传:

A.send_keys()

Python selenium webdriver GUI自动化测试Selenium webdriver常用操作

B.使用Autolt

请见文件:GUI自动化测试_文件处理之Autolt.xmind

(5)鼠标事件:

(1)鼠标悬浮到指定元素上方,并点击出现的对应元素信息的操作:

from selenium.webdriver.common.action_chains import ActionChains
top_menu = wd.find_element_by_xpath(data["path"])
ActionChains(wd).move_to_element(top_menu).perform()
time.sleep(2)
wd.find_element_by_xpath(data["menu_path"]).click()
           

(2)鼠标右键操作:

#定位到要右击的元素
qqq=driver.find_element_by_xpath("path")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(qqq).perform()
           

(3)鼠标拖放操作:

#定位元素的原位置
element = driver.find_element_by_name("source")
#定位元素要移动到的目标位置
target =  driver.find_element_by_name("target")
#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()
           

(4)鼠标双击操作:

#定位到要双击的元素
qqq =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标双击
ActionChains(driver).double_click(qqq).perform()
           

(6)下拉框几种方式:

A.随机选择下拉框任意一个

1)随机选择下拉框任意一个

from selenium.webdriver.support.select import Select

#以列表形式获取下拉框元素对象

ops = Select(wd.find_element_by_xpath("")).options

#遍历对象

for ele in ops:

print(ele.text)

#根据下标随机选择下拉框任意一个

index = random.randrange(0,len(ops)-1)

Select(wd.find_element_by_xpath("")).select_by_index(index)

2)根据下拉框的文本内容选择:

Select(wd.find_element_by_xpath("")).select_by_visible_text(value)

3)根据下标索引选择:

Select(wd.find_element_by_xpath("")).select_by_index()

4)根据下拉框的值选择:

Select(wd.find_element_by_xpath("")).select_by_value()

B.如果下拉框为列表可以先获取整个下拉框并放入一个列表中,再根据索引选择下拉框的值

li = wd.find_element_by_xpath("/html/body/div[4]/div[1]/div[1]/ul")
li[1].click()
           

C.可以先点开下拉框,再点击需求选取的值

wd.find_element_by_css_selector('.el-input__inner').click()
wd.find_element_by_css_selector('[x-placement='bottom-start']li:nth-of-type(2)').click()
           

(7)键盘事件:

from selenium.webdriver.common.keys import Keys
if data["manipulate"] == "ENTER":
		wd.find_element_by_xpath(data['path']).send_keys(Keys.ENTER)
if data["manipulate"] == "ESC":
		wd.find_element_by_xpath(data["path"]).send_keys(Keys.ESCAPE)
           

(8)xpath绝对路径和相对路径: 尽量不使用绝对路径

1)绝对路径:从根目录一层一层的往下找元素

/html//div[@id=‘app’]/div[@class=‘login_wrapper’]//div[@class=‘el-row’]//form[@class=‘el-form’]//input[@type=‘text’]

2)相对路径:先找对应的标签,再通过该标签下的属性寻找元素,多个属性用and连接

//标签[@属性=” ” and @属性=” “]

(9)获取元素的文本信息:

(1)text:适用于表单元素,例如:文本域、下拉框、文本框、按钮、弹出框等

Python selenium webdriver GUI自动化测试Selenium webdriver常用操作

(2)get_attribute(属性名):除了.text其他均可使用

Input标签默认有value属性,可以通过get_attribute(value)获取

Python selenium webdriver GUI自动化测试Selenium webdriver常用操作