selenium

本文演示了如何利用Python的Selenium库进行Web自动化测试,包括打开浏览器、页面导航、元素定位、断言验证、页面操作以及窗口管理等。此外,还展示了使用Pytest进行测试类的编写,实现测试用例的组织和执行。
from selenium import webdriver #导入selenium
huohu = webdriver.Firefox()    #打开浏览器
huohu.maximize_window()        #最大化
huohu.get('http://www.baidu.com')  #打开网址、项目
# huohu.get_window_size()  #获取浏览器尺寸
# huohu.set_window_size()  #设置浏览器尺寸
# huohu.get_window_position() #获取浏览器位置
# huohu.set_window_position(x,y) #设置浏览器位置
# 页面请求操作
# huohu.refresh()           #刷新页面操作
# huohu.back()          #回退到之前的页面
# huohu.forward()           #前进到之后的页面
# 3.获取断言信息
# 什么是断言?
# 断言是编程术语,表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真,可以在任何时候启用和禁用断言验证,因此可以在测试时启用断言而在部署时禁用断言。
# 获取断言信息的操作
# print(huohu.current_url)  #获取当前访问页面url
# print(huohu.title)        #获取当前浏览器标题
# 保存图片
# with open("1.png","wb") as f:
#     f.write(huohu.get_screenshot_as_png())
# print(huohu.page_source)   #查看网页源码
# 第一种定位方式
# huohu.find_element_by_id('wd')
#第二种定位方式
# huohu.find_elements_by_name("qwe")
# 第三种定位方式
# huohu.find_element_by_class_name("a")
# 第四种定位方式
# huohu.find_element_by_tag_name("vic")
# 第五种定位方式
# huohu.find_element_by_link_text("新闻")
# 第六种定位方式
# huohu.find_element_by_partial_link_text("新")
#第七种定位方式 xpath
# huohu.find_element_by_xpath("//*")
# 第八种定位方式 css
# huohu.find_elements_by_css_selector('#wd')
# 直接调用 id
# huohu.find_element("kw")
# 直接调用 name
# huohu.find_element("name")
# )获取所有窗口的句柄
# handles = driver. window_handles
# driver.switch_to_window(handles[n])
# 直接使用id值切换进表单,
# driver.switch_to.frame(value)
# 定位到表单元素,再切换进入
# el = driver.find_element_by_xxx(value)
# driver.switch_to.frame(el)
srk = huohu.find_element_by_css_selector("#kw")
srk.send_keys("10086")  #输入数据
bdyx = huohu.find_element_by_xpath("//*[@id='su']")
bdyx.click()  #点击
# bdyx.clear()  #清空
# huohu.quit()   #关闭所有标签/窗口
# huohu.close()  #关闭当前标签/窗口

from selenium import webdriver
import pytest,allure,os,time
class Testclass():
    @classmethod
    def setup_class(cls):  #开启
        cls.huohu = webdriver.Chrome('Chrome/Application/chromedriver.exe')
        cls.huohu.get("https://www.baidu.com/")
        aa = cls.huohu.find_element_by_xpath('//*[@id="s-top-left"]/a[1]')
        assert aa.text=="新闻"
    def test001(self):
        self.huohu.find_element_by_xpath('//*[@id="s-top-left"]/div/a').click()
        self.huohu.switch_to.window(self.huohu.window_handles[1])
        aa = self.huohu.find_element_by_xpath('//*[@id="head"]/div[2]/a[1]')
        assert aa.text=="百度首页"
    def test002(self):
        huohu = self.huohu
        huohu.find_element_by_xpath('//*[@id="content"]/div[15]/div[2]/a').click()
        huohu.switch_to.window(huohu.window_handles[2])
        aa = huohu.find_element_by_xpath('//*[@id="index-page-container"]/div[3]/div[1]/div[1]')
        assert aa.text=="日常快译"
    def test003(self):
        huohu = self.huohu
        huohu.find_element_by_xpath('//*[@id="whole-page-header"]/div/ul/li[2]/a').click()
    @classmethod
    def teardown_class(cls):
        time.sleep(10)
        cls.huohu.quit()   #关闭
if __name__ == '__main__':
    pytest.main(['tzlx.py'])

### Selenium 使用指南 #### Selenium 的发展历史与主要组成部分 Selenium 3于2009年发布,这一版本去除了Selenium RC,其核心组件主要包括Selenium WebDriver 和 Selenium Grid[^1]。其中,Selenium WebDriver 是我们日常开发中最常用的模块,而 Selenium Grid 则用于支持分布式环境下的自动化测试。 --- #### Selenium 的安装方法 为了在 Python 中使用 Selenium 进行浏览器操作,需先完成以下准备工作: 1. **安装 Selenium 库** 可通过 `pip` 命令来安装 Selenium: ```bash pip install selenium ``` 2. **下载对应的浏览器驱动程序** 不同的浏览器需要不同的驱动程序。例如,如果使用的是 Microsoft Edge 浏览器,则需要在其官方页面上查找并下载与当前浏览器版本匹配的驱动程序[^3]。具体步骤如下: - 打开浏览器设置界面,确认当前浏览器的版本号。 - 访问 [Microsoft Edge WebDriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) 页面,选择与之相匹配的驱动文件进行下载。 - 将下载好的驱动放置到系统的 PATH 路径下或者指定路径供脚本调用。 --- #### Selenium 的基本使用流程 以下是利用 Selenium 实现简单网页浏览的一个示例代码片段: ```python from selenium import webdriver from selenium.webdriver.edge.service import Service as EdgeService # 设置Edge驱动服务对象 service = EdgeService(executable_path="path/to/msedgedriver") # 初始化WebDriver实例 driver = webdriver.Edge(service=service) try: # 打开目标网站 driver.get("http://www.example.com") # 获取页面标题 title = driver.title # 输出页面标题 print(f"Page Title is {title}") finally: # 关闭浏览器窗口 driver.quit() ``` 上述代码展示了如何加载一个简单的网页,并获取该网页的标题信息[^2]。 --- #### Selenium 的应用场景 Selenium 是一款非常强大的工具,广泛应用于以下几个领域: - 自动化测试:验证 Web 应用的功能是否正常运行。 - 数据抓取:模拟真实用户的交互行为从而提取所需的数据。 - UI/UX 验证:检查前端布局以及用户体验是否存在异常情况。 --- 问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值