• Selenium01


    1:selenium

    Selenium主要用于web应用程序自动化测试,Selenium直接运行在浏览器中,就像实际用户在操作浏览器

    三大功能:

    • Selenium Webdriver,最重要的模块
    • Selenium IDE,录屏,将网页上的操作过程录制为测试用例并能导出测试脚本
    • Selenium Grid,分布式测试

    selenium特点:

    • 功能强大
    • 跨平台,Windows、linux、Mac
    • 支持多种浏览器:Chrome、Firefox、Safari、Opera、IE、Edge等
    • 支持多种开发语言:Python、Java、C#、Ruby,JavaScript
    • 简单、灵活
    • 支持分布式测试

    安装Python的selenium模块:

    pip install selenium
    
    • 1

    浏览器驱动下载:

    • 从镜像网站(https://registry.npmmirror.com/binary.html)查找对应的版本号链接
    • 将下载到的zip文件保存到任何一个PATH环境变量指定的目录(安装python解释器时已将其路径添加到PATH了,可以放到此路径下)下解压缩即可。

    导入依赖:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    • 1
    • 2

    创建浏览器对象:

    driver = webdriver.Chrome()
    
    • 1

    get方法打开指定页面:

    driver.get('http://www.baidu.com')
    
    • 1

    关闭

    # 关闭浏览器窗口,进程也会存在驱动运行
    driver.close()
    # 退出浏览器驱动
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4

    2:八大定位方式

    • ID定位

      • driver.find_element(By.ID, 'kw').send_keys('selenium')#文本框输入
        driver.find_element_by_id('su').click()#点击
        
        • 1
        • 2
    • NAME定位

      • driver.find_element(By.NAME,'wd').send_keys('西安天气')
        
        • 1
    • CLASS_NAME定位

      • driver.find_element(By.CLASS_NAME,'el-icon-arrow-down').click()
        
        • 1
    • TAG_NAME定位

      • driver.find_element(by=By.TAG_NAME,value='input')
        
        • 1
    • LINK_TEXT定位

      • driver.find_element(by=By.LINK_TEXT,value='新闻').click()
        
        • 1
    • PARTIAL_LINK_TEXT定位

      • driver.find_element(by=By.PARTIAL_LINK_TEXT,value='新').click()
        
        • 1
    • XPATH定位

      • #绝对Xpath
        driver.find_element(by=By.XPATH,value='/html/body/div[1]/div[1]/div[5]/div[1]/div/form/span[1]/input').send_keys('SDK')
        #相对Xpath
        driver.find_element(by=By.XPATH,value='//*[@id="kw" and @name="wd"]').send_keys("sdk")
        #利用属性进行定位    @ 选取属性,id为一个属性,=后面是属性的值 属性之间可以进行逻辑运算 and or not
        driver.find_element_by_xpath('//*[@name="wd" and @class="s_ipt"]')
        #利用文本精确定位
        driver.find_element_by_xpath('//*[text()="新闻"]')
        #利用文本模糊定位
        driver.find_element_by_xpath('//*[contains(@value,"百度")]')
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
    • CSS_SELECTOR定位

      • driver.find_element(by=By.CSS_SELECTOR,value='#kw').send_keys("sdk")
        
        • 1

    3:常用API

    获取属性:

    element.get_attribute('href')
    element.get_attribute('value')
    
    • 1
    • 2

    获取文本:

    element.text
    
    • 1

    窗口最大化:

    driver.maximize_window()
    
    • 1

    窗口最小化:

    driver.minimize_window()
    
    • 1

    获取窗口句柄:

    driver.window_handles
    
    • 1

    切换到指定窗口:

    driver.switch_to.window(handles[1])
    
    • 1

    进入iframe:

    driver.switch_to.frame(iframe)
    
    • 1

    退出iframe:

    driver.switch_to.parent_frame()
    
    • 1

    关闭当前窗口:

    driver.close()
    
    • 1

    退出浏览器:

    dirver.quit()
    
    • 1

    后退/前进/刷新:

    driver.back()
    driver.forward()
    driver.refresh()
    
    • 1
    • 2
    • 3

    配置无框浏览器:

    ops = webdriver.ChromeOptions()
    ops.add_argument('headless')
    driver = webdriver.Chrome(options=ops)
    
    • 1
    • 2
    • 3

    左键单击:

    ActionChains(driver).click(element).perform()
    
    • 1

    右键单击:

    ActionChains(driver).context_click(input).perform()
    
    • 1

    左键双击:

    ActionChains(driver).double_click(back).perform()
    
    • 1

    鼠标悬停:

    ActionChains(driver).move_to_element(more).perform()
    
    • 1

    键盘操作:

    #回车
    input.send_keys(Keys.ENTER)
    #ctrl+a
    input.send_keys(Keys.CONTROL, 'a')
    #ctrl+c
    input.send_keys(Keys.CONTROL, 'c')
    #ctrl+v
    input.send_keys(Keys.CONTROL, 'v')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    断言:

    assert 条件 ,"断言失败信息"
    
    • 1

    下拉框选择:

    #索引值选择
    Select(select).select_by_index(index)
    #value值选择
    Select(select).select_by_value(select_value)
    #text文本选择
    Select(select).select_by_visible_text(text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Onnxruntime输入和输出打印
    Python绘图报错解决:The factorplot function has been renamed to catplot.
    Unity中,如何在【编辑器】和【运行时】状态下读写一个ScriptableObject对象
    FL Studio Fruity Edition2024中文入门版Win/Mac
    关于HM NISEDIT在新版系统下编译并运行提示权限不足问题的解决方案
    使用Spring Boot注册整合方式整合Servlet三大组件
    【C#/.NET】使用ASP.NET Core对象池
    HTML制作个人网页制作(简单静态HTML个人博客网页作品)
    ROS2专题【01】:win10上安装ROS2
    JavaScript 清空数组的方法大全
  • 原文地址:https://blog.csdn.net/hd_cash/article/details/126001873