微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UI自动化测试1

一、安装selenium库

打开控制台输入:pip3  install selenium

 

 

 环境搭建

1、查看浏览器版本号

2、在淘宝源下载与版本号对应的程序:

3、下载对应的操作系统

4、把下载好的文件解压后放在python的目录下

 

 

 5、在控制台输入进入python,输入(from selenium import webdriver,driver=webdriver.Chrome())打开一个网页

 

 

6、通过执行代码方式用控制台进入百度浏览器:

7、退出

 

 

 二、分为两类:
1、单个元素的定位
2、多个元素的定位

 八大元素:

ID="id"

XPATH="xpath"

LINK_TEXT="Link text"

PARTIAL_LINK_TEXT="partial link text

NAME="name"

TAG_NAME="tag name"

CLASS_NAME="class name"

CSS_SELECTOR="css selector"

1、通过ID来定位:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t

"""通过ID定位"""
driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.find_element_by_id("kw").send_keys("Holle Word")
t.sleep(5)

  

2、通过name来定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t

"""通过 name来定位"""
driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.find_element_by_name("wd").send_keys("Holle Word")
t.sleep(5)

  

3、通过class_name定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t

"""class_name的定位"""
driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.find_element_by_class_name("s_ipt").send_keys("Holle Word")
t.sleep(5)

 

 

 4、通过xpath定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t


"""xpath定位"""
driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.find_element_by_xpath('//*[@id="kw"]').send_keys("Holle Chen")
t.sleep(5)

  

5、通过css_selector定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t

"""by_css_selector"""
# driver=webdriver.Chrome()
# driver.get('http://www.baidu.com')
# driver.find_element_by_css_selector('#wk').send_keys("Holle Chen")
# t.sleep(5)

  

 

 

 6、 根据link_text定位(准确定位)

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t

"""LINK_TEXT"""
driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.find_element_by_link_text('书籍').click()
t.sleep(5)

  

 7、根据partial_link_text定位(模糊定位)

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t


"""PARTIAL_LINK_TEXT"""
# driver=webdriver.Chrome()
# driver.get('http://www.baidu.com')
# driver.find_element_by_partial_link_text('图').click()
# t.sleep(5)

8、多个元素定位:

通过 tag_name定位

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t

driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
tag=driver.find_elements_by_tag_name("input")
tag[7].send_keys("qwe")
t.sleep(5)
driver.quit()

  按照索引获取

 三、元素定位方法的案例实战(网易,sina邮箱)

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t


driver=webdriver.Chrome()
driver.get('https://mail.sina.com.cn/')
#输入邮箱
driver.find_element_by_id('freename').send_keys("chengxin@163.com")
t.sleep(5)
#输入密码
driver.find_element_by_id('freepassword').send_keys("chengxin")
t.sleep(5)

 执行结果如下:

 

四、webdriver的浏览器属性方法应用

from selenium import webdriver
from selenium.webdriver.common.by import By
import time as t


"""webdriver操作浏览器的属性"""
# driver=webdriver.Chrome()
# driver.get('http://www.baidu.com')
# print('获取当前的地址:',driver.current_url)
# print('获取title;',driver.title)
# print('获取当前页面的源码:',driver.page_source)
# t.sleep(5)
# driver.get("http://www.sina.com.cn/")
# t.sleep(5)
# #后退
# driver.back()
# t.sleep(5)
# driver.forward()
# t.sleep(5)
# print('获取执行的浏览器:',driver.name)
# driver.quit()

  

五、结合函数对元素定位方法封装

"""使用函数的方式"""
def ID(driver,content):
	return driver.find_element_by_id(content)

def name(driver,content):
	return driver.find_element_by_name(content)

def className(driver,content):
	return driver.find_element_by_class_name(content)

def tagsName(driver,content,index):
	return driver.find_elements_by_tag_name(content)[index]

def linkText(driver,content):
	return driver.find_element_by_link_text(content)

def partial(driver,content):
	return driver.find_element_by_partial_link_text(content)

def css(driver,content):
	return driver.find_element_by_css_selector(content)

def xpaths(driver,content):
	return driver.find_element_by_xpath(content)

driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
# ID(driver,'kw').send_keys("holle 陈")
# driver.find_element_by_xpath()
# xpaths(driver,'//*[@id="kw"]').send_keys("dasd")

# tagsName(driver,'input',7).send_keys("holle chen")
# className(driver,'s_ipt').send_keys("cheng")
# linkText(driver,'直播').click()
# partial(driver,'图').click()
# css(driver,'#kw').send_keys("chengxi")
# name(driver,'wd').send_keys("chen")

t.sleep(3)
driver.quit()

  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐