• Selenium4.0+Python3系列(四) 常见元素操作(含鼠标键盘事件)


    一、写在前面

    上篇文章介绍的是关于浏览器的常见操作,接下来,我们将继续分享关于元素的常见操作,建议收藏、转发!

    二、元素的状态

    在操作元素之前,我们需要了解元素的常见状态。

    1、常见元素状态判断,傻傻分不清

    • is_displayed()
    • is_enabled()
    • is_selected()

    2、is_displayed()

    判断元素是否显示

    element.is_displayed()
    

    注意:

    判断button是否显示,和is_displayed()容易混淆的是is_enabled()

    区别在于,直接用element.is_enabled()方法判断button是否显示,返回值为true,因为button是使用CSS方法判断是否有效,这并不是真正的方法,需要判断其class中是否有值为disabled来判断是否真正处于disabled的状态.

    3、is_enabled()

    判断元素是否有效,即是否为灰化状态

    element.is_enabled()  
    

    4、is_selected()

    一般判断表单元素,如radio或checkbox是否被选中。

    element.is_selected() 
    

    三、常见元素的操作

    这部分主要演示的常见点击操作,例如:文本输入、复选框、单选按钮、选择选项、鼠标点击事件等等。

    1、元素点击操作

    演示案例:

    点击(鼠标左键)页面按钮click()

    示例代码如下:

    driver.get("http://localhost:8080/click.html")
    button1 = driver.find_element(By.ID, "button1")
    is_displayed = button1.is_enabled()
    if is_displayed:
        button1.click()
    

    2、Submit操作

    演示案例:

    点击(鼠标左键)页面按钮submit()

    示例代码如下:

    driver.get("http://localhost:8080/submit.html")
    login = driver.find_element(By.ID, "login")
    is_displayed = login.is_enabled()
    if is_displayed:
        login.submit()
        # login.click()
    

    小贴士:

    支持submit的肯定支持click,但是支持click的,不一定支持submit,可能会报错如下:

    3、输入、清空输入操作

    演示案例:

    输入、清空输入操作clear(), send_keys()

    示例代码如下:

    username = driver.find_element(By.CSS_SELECTOR, "input[type='text']")
    username.clear()
    username.send_keys(u"公众号:软件测试君")
    # 输出:公众号:软件测试君
    print('输入值:{0}'.format(username.get_attribute("value")))
    time.sleep(1)
    

    四、鼠标键盘事件操作

    1、模拟回车操作

    模拟打开百度搜索输入博客园,回车操作,示例代码如下:

    driver.get("https://www.baidu.com/")
    driver.find_element(By.ID, "kw").send_keys("久曲健 博客园", Keys.ENTER)
    

    2、常见鼠标操作

    演示案例:

    常见鼠标操作很多,如左键点击、悬浮、移动、双击、右键等等,示例代码如下:

    driver.get("http://localhost:8080/mouse.html")
    # 鼠标左键点击
    ActionChains(driver).click(driver.find_element(By.ID, "mouse2")).perform()
    time.sleep(1)
    driver.switch_to.alert.accept()
    time.sleep(1)
    # 鼠标悬浮并移动操作
    ActionChains(driver).move_to_element(driver.find_element(By.ID, "mouse1")).pause(1).move_to_element(
        driver.find_element(By.ID, "mouse6")).perform()
    time.sleep(1)
    driver.switch_to.alert.accept()
    # 鼠标双击操作
    ActionChains(driver).double_click(driver.find_element(By.ID, "mouse3")).perform()
    time.sleep(1)
    driver.switch_to.alert.accept()
    # 鼠标右键
    ActionChains(driver).context_click(driver.find_element(By.ID, "mouse5")).perform()
    

    3、常见的键盘操作

    键盘操作 对应代码
    键盘F1到F12 send_keys(Keys.F1) 把F1改成对应的快捷键
    复制Ctrl+C send_keys(Keys.CONTROL,'c')
    粘贴Ctrl+V send_keys(Keys.CONTROL,'v')
    全选Ctrl+A send_keys(Keys.CONTROL,'a')
    剪切Ctrl+X send_keys(Keys.CONTROL,'x')
    制表键Tab send_keys(Keys.TAB)

    五、演示案例源码

    示例代码:

    # -*- coding: utf-8 -*-
    """
    @Time : 2022/10/25 21:39
    @Auth : 软件测试君
    @File :element_actions.py
    @IDE :PyCharm
    @Motto:ABC(Always Be Coding)
    
    """
    import time
    
    from selenium.webdriver import Keys, ActionChains
    from selenium.webdriver.common.by import By
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    '''
    初始化操作
    '''
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    
    
    def init():
        # 最大化操作
        driver.maximize_window()
        driver.set_script_timeout(60)
        # 智能等待找到元素后立即继续执行,全局生效
        driver.implicitly_wait(60)
        driver.set_page_load_timeout(60)
    
    
    init()
    
    '''
    元素点击操作
    '''
    
    
    def clickDemo():
        # 点击(鼠标左键)页面按钮:click()
        driver.get("http://localhost:8080/click.html")
        button1 = driver.find_element(By.ID, "button1")
        is_displayed = button1.is_enabled()
        if is_displayed:
            button1.click()
    
        # 关闭弹窗
        driver.switch_to.alert.accept()
    
    
    ### 元素基本操作
    clickDemo()
    time.sleep(1)
    
    '''
    submit操作
    '''
    
    
    def submitDemo():
        # 点击(鼠标左键)页面按钮:submit()
        driver.get("http://localhost:8080/submit.html")
        login = driver.find_element(By.ID, "login")
        is_displayed = login.is_enabled()
        if is_displayed:
            login.submit()
            # login.click()
        # 小贴士:支持submit的肯定支持click,但是支持click的,不一定支持submit,可能会报错如下:
    
    
    submitDemo()
    
    '''
    输入、清空输入操作
    '''
    
    
    def clearInputDemo():
        # 输入、清空输入操作:clear() send_keys()
        username = driver.find_element(By.CSS_SELECTOR, "input[type='text']")
        username.clear()
        username.send_keys(u"公众号:软件测试君")
        # 输出:公众号:软件测试君
        print('输入值:{0}'.format(username.get_attribute("value")))
        time.sleep(1)
    
    
    clearInputDemo()
    
    '''
    模拟打开百度搜索输入博客园,回车操作
    '''
    
    
    def mockEnterDemo():
        # 模拟打开百度搜索输入博客园,回车操作 示例代码
        driver.get("https://www.baidu.com/")
        driver.find_element(By.ID, "kw").send_keys("久曲健 博客园", Keys.ENTER)
    
    
    ### 键盘操作
    mockEnterDemo()
    def mouseDemo():
        driver.get("http://localhost:8080/mouse.html")
        # 鼠标左键点击
        ActionChains(driver).click(driver.find_element(By.ID, "mouse2")).perform()
        time.sleep(1)
        driver.switch_to.alert.accept()
        time.sleep(1)
        # 鼠标悬浮并移动操作
        ActionChains(driver).move_to_element(driver.find_element(By.ID, "mouse1")).pause(1).move_to_element(
            driver.find_element(By.ID, "mouse6")).perform()
        time.sleep(1)
        driver.switch_to.alert.accept()
        # 鼠标双击操作
        ActionChains(driver).double_click(driver.find_element(By.ID, "mouse3")).perform()
        time.sleep(1)
        driver.switch_to.alert.accept()
        # 鼠标右键
        ActionChains(driver).context_click(driver.find_element(By.ID, "mouse5")).perform()
    
    
    ###  常见键盘事件操作
    mouseDemo()
    
    time.sleep(3)
    driver.quit()
    

    六、最后

    到此,常见元素操作演示结束,这里只是列举了一些常用的操作,关于其他操作,感兴趣的同学请左键查看源代码 !

    我是六哥,如果觉得文章对您有帮助,建议收藏、转发!

    请继续关注我,我的公众号:软件测试君,并帮忙转发文章到朋友圈。

    你的每一次转发,我都当做了喜欢!

  • 相关阅读:
    封装transform函数(设置和获取transform的属性和属性值)
    layui第三方组件 inputTags 标签输入框
    layui编辑器(layedit)的实现和图片上传功能
    php编写抽奖后台实现抽奖概率计算
    laravel中使用事物
    laravel 使用jwt的基本应用(适于初始jwt)
    layui 下拉框动态添加数据(监听下拉框(select)事件)
    laravel后台账户登录验证(5.5.48版本)
    使用三目运算获取3个数值中最大的数值
    Laravel框架使用融云服务端SDK
  • 原文地址:https://www.cnblogs.com/longronglang/p/16826166.html
Copyright © 2020-2023  润新知