zoukankan      html  css  js  c++  java
  • 鼠标操作、toast、用户输入、快捷键操作

    ## 鼠标悬停
    ```python
    el = browser.find_element('xpath', "//a[contains(text(),'HTML / CSS')]")
    # actionchains 下面动作方法必须要再加上 perform 才能生效
    ActionChains(browser).move_to_element(el).perform()
    import time
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    
    def test_hover():
        url = 'https://www.runoob.com/html/html-tutorial.html'
        with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
            browser.implicitly_wait(5)
            browser.get(url)
    
            # 鼠标悬停(哪个元素)
            el = browser.find_element('xpath', "//a[contains(text(),'HTML / CSS')]")
            # actionchains 下面动作方法必须要再加上 perform 才能生效
            ActionChains(browser).move_to_element(el).perform()
    
            # 双击
            # ActionChains(browser).double_click(el).perform()
            # # 右击
            # ActionChains(browser).context_click(el).perform()
            # # 拖动
            # ActionChains(browser).drag_and_drop(el, el).perform()
    
    
            time.sleep(5)


    # 双击
    # ActionChains(browser).double_click(el).perform()
    # # 右击
    # ActionChains(browser).context_click(el).perform()
    # # 拖动
    # ActionChains(browser).drag_and_drop(el1, el2).perform()
    import time
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    
    def test_drapanddrop():
        url = 'https://demos.telerik.com/kendo-ui/dragdrop/index'
        with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
            browser.implicitly_wait(5)
            browser.maximize_window()
            browser.get(url)
    
            el_start = browser.find_element('id', "draggable")
            # 把该元素挪动到可视范围之内
            el_start.location_once_scrolled_into_view
            el_end = browser.find_element('id', "droptarget")
            # actionchains 下面动作方法必须要再加上 perform 才能生效
            # ActionChains(browser).move_to_element(el).perform()
            # # 拖动
            ActionChains(browser).drag_and_drop(el_start, el_end).perform()
    
    
            time.sleep(5)

    ```

    ## toast
    因为是通过 JS 动态加载的,过3s 消失,当出现时,使用 source 的暂停键,
    debug,
    注意设置等待。

    ## 用户输入,
    - send_keys()
    - 单选
    - 多选 (找到元素,直接点击)
    - select (直接找到 option 元素,点击)
    import time
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    
    def test_check():
        url = 'file:///D:/vip%E7%8F%AD%E7%BA%A7/py44/day37_%E6%B5%8F%E8%A7%88%E5%99%A8%E6%93%8D%E4%BD%9C/radio.html'
        with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
            browser.implicitly_wait(5)
            browser.maximize_window()
            browser.get(url)
    
            browser.find_element('xpath', '//input[@value="a"]').click()
            # time.sleep(5)
    
    
            # 下拉框
            # browser.find_element('name', 'mysel').click()
            browser.find_element('xpath', '//option[text()="选项3"]').click()
            time.sleep(5)

    - 快捷键
    ```python
    # 回车
    browser.find_element('id', 'kw').send_keys('困困', Keys.ENTER)
    import time
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains, Keys
    
    
    def test_kuaijiejian():
        url = 'http://www.baidu.com'
        with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
            browser.implicitly_wait(5)
            browser.maximize_window()
            browser.get(url)
    
            browser.find_element('id', 'kw').send_keys('困困')
            # 回车
            # browser.find_element('id', 'kw').send_keys(Keys.ENTER)
    
            # 第二种情况:  Ctrl + c (+Ctrl松开)  Ctrl + v
            # 下面这个是先按control,松开后再v
            # browser.find_element('id', 'kw').send_keys(Keys.CONTROL, 'v')
    
            actions = ActionChains(browser)
            # 3个步骤,组合键,先按control再按a再抬起control
            actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
            actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
            actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
            actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
            actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
            time.sleep(5)
  • 相关阅读:
    【大厂面试06期】谈一谈你对Redis持久化的理解?
    【大厂面试05期】说一说你对MySQL中锁的了解?
    【大厂面试04期】讲讲一条MySQL更新语句是怎么执行的?
    【大厂面试03期】MySQL是怎么解决幻读问题的?
    【大厂面试02期】Redis过期key是怎么样清理的?
    【大厂面试01期】高并发场景下,如何保证缓存与数据库一致性?
    透过面试题掌握Redis【持续更新中】
    MySQL慢查询优化(线上案例调优)
    分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo+SkyWalking的微服务开发框架
    微服务框架Demo.MicroServer运行手册
  • 原文地址:https://www.cnblogs.com/wsfsd/p/15623598.html
Copyright © 2011-2022 走看看