下载python
阿里云盘下载 https://www.aliyundrive.com/s/yZrNY8vASDe 官网下载 https://www.python.org/downloads/windows/
python安装
配置环境变量
环境变量PATH 中配置 Python按照路径 验证Python是否按照成功 输入python 配置pip环境变量 python安装路径下的Scripts路径配置到环境变量中 验证配置是否成功 cmd中输入pip
下载安装selenium
cmd 命令中 输入 pip list 可以查看已经安装好的Python插件 输入 pip install selenium 进行安装
安装PyCharm
阿里云盘下载 https://www.aliyundrive.com/s/yZrNY8vASDe
PyCharm设置 选择系统安装Python的路径 hello world 案例测试
右键运行
下载安装浏览器驱动
下载自己谷歌浏览器对应版本的驱动 例如我下载的是这个版本 若是其他的浏览器 就下载对应版本的驱动 把下载的好的驱动地址放到本地电脑的此目录下 C:\Program Files\selenium_driver
配置环境变量 验证驱动 环境变量是否配置成功
第一个selenium测试
新建文件 demo01 源代码如下:
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
sleep(1)
driver.find_element('kw').send_keys('selenium')
sleep(1)
driver.find_element('su').click()
sleep(3)
driver.quit()
此案例运行结果: 打开Chrome浏览器 打开百度 输入关键词 selenium 点击搜索 退出 的操作
selenium之元素定位
源码测试: demo02.py
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com')
self.driver.maximize_window()
sleep(1)
def test_id(self):
eLement = self.driver.find_element.by_id('kw')
element.send._keys('selenium')
print(type(element))
self.driver.find_element_by_id('su').click()
sleep(3)
def test_name(self):
self.driver.find.element_ by_name( 'wd').send.keys('selenium')
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_linktext(self):
self.test_id()
self.driver.find.element_by_link_text('百度首页').click()
sleep(3)
def test_partial_link_text(self):
self.test_ id()
self.driver.find_ element_by_partial_link_text('首页').click()
sleep(3)
def test_xpath(self):
self.driver.find_element_by_xpath('//*[@id="kw"]').send.keys('极客时间')
self.driver.find_element_by_id('su').click()
sleep(3)
def test_tag(self):
input = self.driver.find.element.by_.tag.name('input')[0]
print(input)
def test_css_selector(self):
self.driver.find_element_by_css_selector('#kw').send_keys('极客时间')
self.driver.find_element_by_id('su').click()
sleep(3)
def test_class_name(self):
self.driver.find_element_by_class.name('s_ipt').send_keys('极客时间')
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_all(self):
self.driver.find_ element(By.ID,value='kw').send.keys('selenium')
self.driver.find_eLement_ by_ id('su').click()
sleep(3)
self.driver.quit()
if _ _name_ _=='_ _main_ _':
case = TestCase()
练习demo
理解WebDriver运行原理
selenium webdriver 属性
源码 demo03.py
from selenium import webdriver
from time import sleep
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com')
self.driver.maximize.window()
def test_prop(self):
print(self.driver.name)
print(self.driver.current_url)
print(self.driver.title)
print(self.driver.window_handles)
print(self.driver.page_source)
self.driver.quit()
def test_method(self):
self.driver.find_eLement_by_id('kw').send.keys('selenium')
self.driver.find_element_by_id('su').click()
sleep(2)
self.driver.back()
sleep(2)
self.driver.refresh()
sleep(2)
self.driver.forward()
self.driver.close()
self.driver.quit()
def test_windows(self):
self.driver.find_element_by_link_text('新闻').click()
windows = self.driver.window_handles
while 1:
for w in windows:
self.driver.switch.to.window(w)
sleep(2)
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
运行结果:
Selenium WebElement属性
源码 demo04.py
from selenium import webdriver
from time import sleep
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://sahitest.com/demo/')
self.driver.maximize.window()
def test_webelement_prop(self) :
e = self.driver.find_element_by_id('t1')
e1 = WebElement;
print(type(e))
print(e.id)
print(e.tag_name )
print(e.size)
print(e.rect)
print(e.text)
def test_webelement_method(self):
e = self.driver.find_element_by_id('t1')
e.send_keys('hello world')
sleep(2)
print(e.get_attribute('type'))
print(e.get_attribute('name'))
print(e.get_attribute('value'))
print(e.value_of_css_property('font'))
print(e.value_of_css_property('color'))
sleep(2)
e.clear()
def test_method2(self):
form_element = self.driver.find_element_by_xpath('/html/body/form[1]')
form_element.find_element_by_id('t1' ).send_keys('bala')
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
输出结果 test_webelement_prop: 输出结果 test_webelement_method:
Selenium 操作 form 表单
本地新建html页面 源码 demo05.py
from selenium import webdriver
import os
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
path = os.path.dirname(os.path.abspath(_ _file_ _))
file_path = 'file:///'+path+'/forms.html'
print(file_path)
self.driver.get(file_path)
def test_login(self):
username = self.driver.find_element_by_id('username')
username.send_keys('admin')
pwd = self.driver.find_element_by_id('pwd')
pwd.send_keys('123')
print(username.get_attribute('value'))
print(pwd.get_attribute('value'))
sleep(2)
self.driver.find_element.by_id('submit').click()
self.driver.switch_to.alert.accept()
username.clear()
pwd.clear()
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
case.test_login()
运行结果:
掌握checkbox radiobutton的定位技巧
新建html页面 源码 demo06.py
from selenium import webdriver
from time import sleep
import os
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
path = os.path.dirname(os.path.abspath(_ _file_ _))
file_path = 'file:///'+path+'/forms2.html'
print(file_path)
self.driver.get(file_path)
def test_checkbox(self):
swimming = self.driver.find_element_by_name('swimming')
if not swimming.is_selected():
swimming.click()
reading = self.driver.find_eLement_by.name('reading')
if not reading .is_selected():
reading .click()\
sleep(3)
swimming.click()
sleep(5)
self.driver.quit()
def test_radio(self):
lst = self.driver.find_elements_by_name('gender')
lst[1].click()
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
case.test_radio()
Selenium操作下拉列表
新建html页面
源码 demo07.py
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.support.select import Select
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
path = os.path.dirname(os.path.abspath(_ _file_ _))
file_path = 'file:///'+path+'/forms3.html'
print(file_path)
self.driver.get(file_path)
def test_select(self):
se = self.driver.find_element.by_id('provise')
select= Select(se)
select.select_by_index(2)
sleep(2)
select.select_by_value('bj')
sleep(2)
select.select_by_visible_text('Tianjin')
sleep(2)
self.driver.quit()
def test_mutiselect(self):
se = self.driver.find_element.by_id('provise')
select = Select(se)
for i in range(3):
select.select_by_index(i)
sleep(1)
sleep(3)
select.deselect_all()
sleep(3)
self.driver.quit()
def test_option(self):
se = self.driver.find_element.by_id('provise')
select = Select(se)
for option in select.options:
print(option.text)
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
测试多选下拉框
Selenium处理弹框
新建页面 源码 demo08.py
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.support.select import Select
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
path = os.path.dirname(os.path.abspath(_ _file_ _))
file_path = 'file:///'+path+'/test_alert.html'
print(file_path)
self.driver.get(file_path)
def test_alert(self):
self.driver.find_element_by_id('alert').click()
alert = self.driver.switch_to.alert
print(alert.text)
sleep(3)
alert.accept()
def test_confirm(self):
self.driver.find_element_by.id('confirm').click()
confirm = self.driver.switch_to.alert
print(confirm.text)
sleep(3)
confirm.dismiss()
def test_prompt(self):
self.driver.find_element_by_id('prompt').click()
sleep(2)
prompt = self.driver.switch_to.alert
print(prompt.text)
sleep(2)
prompt.accept()
sleep(5)
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
case.driver.quit()
Selenium三种等待方式:剖析原理、用法和应用场景
源码 demo09.py
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com')
def test_sleep(self):
self.driver.find.element_by_name('wd').send.keys('selenium')
sleep(2)
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_implicitLy(self):
self.driver.implicitly.wait(10)
self.driver.find.element_by_name('wd').send.keys('selenium')
self.driver.find_element_by_id('su').click()
self.driver.quit()
def test_wait(self):
WebDriverWait(self.driver,2)
wait.until(EC.title_is('百度一下,你就知道'))
self.driver.find.element_by_name('wd').send.keys('selenium')
self.driver.find_element_by_id('su').click()
self.driver.quit()
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
Selenium 等待条件
新建页面 源码 demo10.py
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
path = os.path.dirname(os.path.abspath(_ _file_ _))
file_path = 'file:///'+path+'/test_wait.html'
print(file_path)
self.driver.get(file_path)
def test(self):
self.driver.find.element_by_id('btn').click()
wait = WebDriverWait(self.driver,3)
wait.until(EC.text_to_be_present_in_element((By.ID, 'id2'),'id 2'))
print(self.driver.find_element_by_id('id2').text)
print('ok')
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
Selenium 鼠标和键盘事件
源码 demo11.py
from selenium import webdriver
from time import sleep
from seleniun.webdriver import ActionChains
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://sahitest.com/demo/')
self.driver.maximize.window()
def test_mourse(self):
btn = self.driver.find.element_by_xpath('/html/body/form/input[2]')
ActionChains(self.driver).double.click(btn).perform()
sleep(2)
btn = self.driver.find.element_by_xpath('/html/body/form/input[3]')
ActionChains(self.driver).click(btn).perform()
sleep(2)
btn = self.driver.find.element_by_xpath('/html/body/form/input[4]')
ActionChains(self.driver).context.click(btn).perform()
sleep(5)
def test_key(self):
self.driver.get('http://www.baidu.com')
kw = self.driver.find_element_by_id('kw')
kw.send_keys('selenium')
kw.send.keys(Keys.CONTROL,'a')
sLeep(2)
kw.send_keys(Keys.CONTROL,'x')
sleep(2)
kw.send.keys (Keys.CONTROL,'v')
sleep(2)
e = self.driver.find_element_by_link_text('新闻')
print(e)
ActionChains(self.driver).move.to.element(e).click(e).perform()
sleep(2)
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
Selenium 执行JavaScript 脚本
源码 demo12.py
from selenium import webdriver
from time import sleep
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com/')
self.driver.maximize.window()
def test1(self):
self.driver.execute.script("alert('test')")
sleep(2)
self.driver.switch_to.alert.accept()
def test2(self):
js = 'return document.title'
title = self.driver.execute.script(js)
print(title)
def test3(self):
js = 'var q = document.getELementById("kw"); q.style.border="2px solid red"'
self.driver.execute_script(js)
def test4(self):
self.driver.find.element_by_name('wd').send.keys('selenium')
sleep(2)
self.driver.find_element_by_id('su').click()
sleep(2)
js = 'window.scroLlTo(0,document.body.scrollHeight)'
self.driver.execute_script(js)
sleep(2)
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
Selenium屏幕截图
源码 demo13.py
from selenium import webdriver
from time import sleep,strftime,localtime
import os
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com/')
self.driver.maximize.window()
def test1(self):
self.driver.find.element_by_name('wd').send.keys('selenium')
sleep(2)
self.driver.find_element_by_id('su').click()
sleep(2)
st = strftime ("%Y-%m-%d-%H-%M-%S",localtime(time()))
fime_name = st + '.png'
self.driver.save_screenshot(fime_name)
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
selenium 定位frame 和 iframe
源码 demo12.py
from selenium import webdriver
from time import sleep
class TestCase(object):
def _ _init_ _(self):
self.driver = webdriver.Chrome()
self.driver.get('http://sahitest.com/demo/framesTest.htm')
self.driver.maximize.window()
def test1(self):
top = self.driver.find_element_by.name('top')
self.driver.switch_to.frame(top)
self.driver.find.element_by_xpath('/html/body/table/tbody/tr/td[1]/a[1]').click()
self.driver.switch_to.default_content()
sleep(3)
second = self.driver.find.element_by_xpath('/html/franeset/frame[2]')
self.driver.switch_to.frame(second)
self.driver.find.element_by_xpath('/html/body/table/tbody/tr/td[1]/a[2]').click()
sleep(5)
self.driver.quit()
if _ _name_ _ == '_ _ main_ _':
case = TestCase ()
|