selenium学习笔记

selenium学习笔记,第1张

下载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):
		# id是唯-的
		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)
		# self.driver.quit()
	
	def test_name(self): 
		# find_element_by_name 方法可能返回多个元素,返回第一个
		# self.driver.find_elements_by_name() 返回一个集合
		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):
	    # 网页界面检查定位元素 copy Xpath获取定位
		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):
		# 网页界面检查定位元素 copy selector获取定位
		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(value='kw').send.keys('selenium')
		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()
	# case.test_id()
	# case.test_name()
	# case.test_linktext()
	# case.test_partial_link_text()
	# case.test_xpath()
	# case.test_tag()
	# case.test_css_selector()
	# case.test_class_name()
	# case.test_all()

练习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) # 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() #只关闭当前tab
		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 ()
	#case.test_prop()
	#case.test_method()
	#case.test_windows()

运行结果:

Selenium WebElement属性



源码 demo04.py

from selenium import webdriver
from time import sleep

# http://sahitest.com/demo/
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()

	#此方法验证 可以通过Xpath方式先找到表单 在通过表单找到里面的输入框
	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 ()
	#case.test_webelement_prop()
	#case.test_webelement_method()
	#case.test_method2() 

输出结果 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) #打印html页面位置路径
		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() # 取消alertd窗
		
		# 清空输入框
		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) #打印html页面位置路径
		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[0].click()
		lst[1].click()

if _ _name_ _ == '_ _ main_ _':
	case = TestCase ()
	#case.test_checkbox()
	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) #打印html页面位置路径
		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 ()
	#case.test_select()
	#case.test_mutiselect()
	#case.test_option()

测试多选下拉框

Selenium处理d框


新建页面

源码 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) #打印html页面位置路径
		self.driver.get(file_path)

	def test_alert(self):
		self.driver.find_element_by_id('alert').click()
		#切换到alert
		alert = self.driver.switch_to.alert #获取到d框
		print(alert.text) #打印d框内容
		sleep(3)
		alert.accept() #相当于d框点击按钮

	def test_confirm(self):
		self.driver.find_element_by.id('confirm').click()
		confirm = self.driver.switch_to.alert
		print(confirm.text)
		# confirm.accept()
		
		sleep(3)
		confirm.dismiss() #d框取消 *** 作

	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.test_alert()
	#case.test_confirm()
	#case.test_prompt()
	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) #现场阻塞 blocking wait
		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')
		#sleep(2) #现场阻塞 blocking wait
		self.driver.find_element_by_id('su').click()
		#sleep(3)
		self.driver.quit()

	def test_wait(self):
	   	WebDriverWait(self.driver,2) #显式等待 每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 ()
	#case.test_sleep()
	#case.test_implicitLy()
	#case.test_wait()
	
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) #打印html页面位置路径
		self.driver.get(file_path)

	def test(self):
		self.driver.find.element_by_id('btn').click()
		#显示等待 等待直到 id为id2的文本值为 id 2 时才继续执行
		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 ()
	#case.test()
	
Selenium 鼠标和键盘事件

源码 demo11.py

from selenium import webdriver
from time import sleep
from seleniun.webdriver import ActionChains

# http://sahitest.com/demo/
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)

	# 模拟打开百度 输入selenium ctrl+a全选 剪切 复制 *** 作
	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 ()
	#case.test_mourse()
	#case.test_key()

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 ()
	#case.test1()
	#case.test2()
	#case.test3()
	#case.test4()

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)
		
		# self.driver.save.screenshot('baidu.png') #截图 保存在当前项目的路径下
		st = strftime ("%Y-%m-%d-%H-%M-%S",localtime(time()))
		fime_name = st + '.png'
		#path = os.path.abspath('scrennshot') #保存截图在 scrennshot文件夹下
		#file_path = path+'/'+fime_name
		self.driver.save_screenshot(fime_name)

if _ _name_ _ == '_ _ main_ _':
	case = TestCase ()
	#case.test1()

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()
     
    # 测试进入frame 然后点击 Link Test超链接
    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]')#获取到第二个frame
		self.driver.switch_to.frame(second) #进入到第二个frame
		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 ()
	#case.test1()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/923101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存