快站app官网下载,联邦快递的网站建设,医院网站建设要求,成都住建局官网有问题怎么办#x1f345; 视频学习#xff1a;文末有免费的配套视频可观看 #x1f345; 点击文末小卡片#xff0c;免费获取软件测试全套资料#xff0c;资料在手#xff0c;涨薪更快 说到自动化测试#xff0c;就不得不提大名鼎鼎的Selenium。Selenium 是如今最常用的自动化测试工… 视频学习文末有免费的配套视频可观看 点击文末小卡片免费获取软件测试全套资料资料在手涨薪更快 说到自动化测试就不得不提大名鼎鼎的Selenium。Selenium 是如今最常用的自动化测试工具之一支持快速开发自动化测试框架且支持在多种浏览器上执行测试。
Selenium学习难度小开发周期短。对测试人员来说如果你编程经验不足python Selenium 是个很好的选择。语法简约清晰可以显著减少后期维护难度和工作压力。
用PythonSelenium做自动化测试可支持多种浏览器爬虫中也可用来解决JavaScript渲染问题。模拟浏览器进行网页加载
今天我们就介绍一下如何用 Selenium 快速开始 Web 测试工作
一、声明浏览器对象
注意点一Python文件名或者包名不要命名为selenium会导致无法导入
from selenium import webdriver
#webdriver可以认为是浏览器的驱动器要驱动浏览器必须用到webdriver支持多种浏览器这里以Chrome为例
browser webdriver.Chrome()
二、访问页面并获取网页html
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.taobao.com)
print(browser.page_source)#browser.page_source是获取网页的全部html
browser.close()
三、查找元素
单个元素
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.taobao.com)
input_first browser.find_element_by_id(q)
input_second browser.find_element_by_css_selector(#q)
input_third browser.find_element_by_xpath(//*[idq])
print(input_first,input_second,input_third)
browser.close()
常用的查找方法
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
也可以使用通用的方法
from selenium import webdriver
from selenium.webdriver.common.by import By
browser webdriver.Chrome()
browser.get(https://www.taobao.com)
input_first browser.find_element(BY.ID,q)#第一个参数传入名称第二个传入具体的参数
print(input_first)
browser.close()
多个元素elements多个s
input_first browser.find_elements_by_id(q)
四、元素交互操作-搜索框传入关键词进行自动搜索
更多操作
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement
#可以有属性、截图等等
browser webdriver.Chrome()
url http://www.runoob.com/try/try.php?filenamejqueryui-api-droppable
browser.get(url)
browser.switch_to.frame(iframeResult)#切换到iframeResult框架
source browser.find_element_by_css_selector(#draggable)#找到被拖拽对象
target browser.find_element_by_css_selector(#droppable)#找到目标
actions ActionChains(browser)#声明actions对象
actions.drag_and_drop(source, target)
actions.perform()#执行动作
更多操作:
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains
五、执行JavaScript
有些动作可能没有提供api比如进度条下拉这时我们可以通过代码执行JavaScript
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.zhihu.com/explore)
browser.execute_script(window.scrollTo(0, document.body.scrollHeight))
browser.execute_script(alert(To Bottom))
六、获取元素信息
获取属性
from selenium import webdriver
from selenium.webdriver import ActionChains
browser webdriver.Chrome()
url https://www.zhihu.com/explore
browser.get(url)
logo browser.find_element_by_id(zh-top-link-logo)#获取网站logo
print(logo)
print(logo.get_attribute(class))
browser.close()
获取文本值
from selenium import webdriver
browser webdriver.Chrome()
url https://www.zhihu.com/explore
browser.get(url)
input browser.find_element_by_class_name(zu-top-add-question)
print(input.text)#input.text文本值
browser.close()
#获取Id位置标签名大小
from selenium import webdriver
browser webdriver.Chrome()
url https://www.zhihu.com/explore
browser.get(url)
input browser.find_element_by_class_name(zu-top-add-question)
print(input.id)#获取id
print(input.location)#获取位置
print(input.tag_name)#获取标签名
print(input.size)#获取大小
browser.close()
七、Frame操作
frame相当于独立的网页如果在父类网frame查找子类的则必须切换到子类的frame子类如果查找父类也需要先切换
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser webdriver.Chrome()
url http://www.runoob.com/try/try.php?filenamejqueryui-api-droppable
browser.get(url)
browser.switch_to.frame(iframeResult)
source browser.find_element_by_css_selector(#draggable)
print(source)
try:
logo browser.find_element_by_class_name(logo)
except NoSuchElementException:
print(NO LOGO)
browser.switch_to.parent_frame()
logo browser.find_element_by_class_name(logo)
print(logo)
print(logo.text)
八、等待
隐式等待
当使用了隐式等待执行测试的时候如果 WebDriver没有在 DOM中找到元素将继续等待超出设定时间后则抛出找不到元素的异常。
换句话说当查找元素或元素并没有立即出现的时候隐式等待将等待一段时间再查找 DOM默认的时间是0
from selenium import webdriver
browser webdriver.Chrome()
browser.implicitly_wait(10)
#等待十秒加载不出来就会抛出异常10秒内加载出来正常返回
browser.get(https://www.zhihu.com/explore)
input browser.find_element_by_class_name(zu-top-add-question)
print(input)
显式等待
指定一个等待条件和一个最长等待时间程序会判断在等待时间内条件是否满足如果满足则返回如果不满足会继续等待超过时间就会抛出异常
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser webdriver.Chrome()
browser.get(https://www.taobao.com/)
wait WebDriverWait(browser, 10)
input wait.until(EC.presence_of_element_located((By.ID, q)))
button wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, .btn-search)))
print(input, button)
title_is 标题是某内容
title_contains 标题包含某内容
presence_of_element_located 元素加载出传入定位元组如(By.ID, p)
visibility_of_element_located 元素可见传入定位元组
visibility_of 可见传入元素对象
presence_of_all_elements_located 所有元素加载出
text_to_be_present_in_element 某个元素文本包含某文字
text_to_be_present_in_element_value 某个元素值包含某文字
frame_to_be_available_and_switch_to_it frame加载并切换
invisibility_of_element_located 元素不可见
element_to_be_clickable 元素可点击
staleness_of 判断一个元素是否仍在DOM可判断页面是否已经刷新
element_to_be_selected 元素可选择传元素对象
element_located_to_be_selected 元素可选择传入定位元组
element_selection_state_to_be 传入元素对象以及状态相等返回True否则返回False
element_located_selection_state_to_be 传入定位元组以及状态相等返回True否则返回False
alert_is_present 是否出现Alert
详细内容
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions
九、前进后退-实现浏览器的前进后退以浏览不同的网页
import time
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.baidu.com/)
browser.get(https://www.taobao.com/)
browser.get(https://www.python.org/)
browser.back()
time.sleep(1)
browser.forward()
browser.close()
十、Cookies
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.zhihu.com/explore)
print(browser.get_cookies())
browser.add_cookie({name: name, domain: www.zhihu.com, value: germey})
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())
选项卡管理 增加浏览器窗口
import time
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.baidu.com)
browser.execute_script(window.open())
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
browser.get(https://www.taobao.com)
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
browser.get(http://www.fishc.com)
十一、异常处理
from selenium import webdriver
browser webdriver.Chrome()
browser.get(https://www.baidu.com)
browser.find_element_by_id(hello)
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
browser webdriver.Chrome()
try:
browser.get(https://www.baidu.com)
except TimeoutException:
print(Time Out)
try:
browser.find_element_by_id(hello)
except NoSuchElementException:
print(No Element)
finally:
browser.close()
# 详细文档
http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions
同时在这我也准备了一份软件测试视频教程含接口、自动化、性能等需要的可以直接在下方观看就行希望对你有所帮助 字节大佬一周讲完自动化测试项目实战这套教程是怎么称霸B站的【2024最新版】