zoukankan      html  css  js  c++  java
  • Python+Selenium笔记(十四)鼠标与键盘事件

     (一) 前言

    Webdriver高级应用的API,允许我们模拟简单到复杂的键盘和鼠标事件,如拖拽操作、快捷键组合、长按以及鼠标右键操作,都是通过使用webdriver的Python API 中的ActionChains类实现的。

    调用ActionChains类方法时,不会立即执行,而是将所有操作都存放在一个队列里,当调用perform()方法时,队列里的操作会依次执行

    (二) 与键盘和鼠标事件有关的一些重要的方法

    方法

    简单说明

    click(on_element=None)

    单击元素

    on_element:指被点击的元素,如果该参数为none,将单击当前鼠标所在位置

    click_and_hold(on_element=None)

    对元素按住鼠标左键

    on_element:指要按住鼠标左键的元素,如果该参数为none,将单击当前鼠标所在位置。

    double_click(on_element=None)

    双击元素

    on_element:只要双击的元素,如果该参数为none,将单击当前鼠标所在位置

    drag_and_drop(source, target)

    鼠标拖动

    Source:鼠标拖动的元素

    Target:鼠标释放的目标元素

    key_down(value, element=None)

    按住某个键,而不释放,用于修饰键(ctrl、alt和shift)

    Vakue:指要按住的键,值在Keys类中定义

    element:指按键触发的目标元素,如果为none,则在当前焦点位置触发。

    key_up(value, element=None)

    释放修饰键

    Vakue:指要按住的键,值在Keys类中定义

    element:指按键触发的目标元素,如果为none,则在当前焦点位置触发。

    move_to_element(to_element)

    将鼠标移到指定元素的中央

    to_element:指定元素

    perform()

    提交已保存的操作

    release(on_element=None)

    释放鼠标

    on_element:被鼠标释放的元素

    send_keys(keys_to_send)

    对当前焦点元素的键盘操作

    keys_to_send:键盘的输入值

    send_keys_to_element(element, keys_to_send)

    对指定元素的键盘操作

    element:指定元素

    keys_to_send:键盘的输入值


    (三) 示例(键盘事件)

     1 from selenium.webdriver.common.action_chains import ActionChains
     2 from selenium import webdriver
     3 from selenium.webdriver.common.keys import Keys
     4 
     5 driver = webdriver.Chrome()
     6 driver.implicitly_wait(20)
     7 driver.maximize_window()
     8 driver.get('https://www.cnblogs.com/')
     9 #找找看搜索框
    10 search_file = driver.find_element_by_css_selector('#zzk_q')
    11 #谷歌搜索框
    12 search_file_gg = driver.find_element_by_css_selector('#google_search_q')
    13 
    14 search_file.send_keys('测试')
    15 #ctrl+a
    16 ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
    17 #ctrl+c
    18 ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
    19 ##ctrl+v
    20 ActionChains(driver).key_down(Keys.CONTROL).send_keys_to_element(search_file_gg,'v').perform()
    21 #ctrl+a
    22 ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
    23 #ctrl+x
    24 ActionChains(driver).key_down(Keys.CONTROL).send_keys('x').key_up(Keys.CONTROL).perform()
    25 #ctrl+v
    26 ActionChains(driver).key_down(Keys.CONTROL).send_keys_to_element(search_file,'v').key_up(Keys.CONTROL).perform()
    27 #回车
    28 ActionChains(driver).send_keys(Keys.ENTER).perform()
    29 
    30 driver.quit()

    (四) 示例(鼠标事件)

     1 from selenium.webdriver.common.action_chains import ActionChains
     2 from selenium import webdriver
     3 
     4 driver = webdriver.Chrome()
     5 driver.implicitly_wait(20)
     6 driver.maximize_window()
     7 driver.get('https://www.cnblogs.com/')
     8 #编程语言
     9 program_lan = driver.find_element_by_xpath('//li[@id="cate_item_2"]/a')
    10 #编程语言下的小类Python
    11 program_py = driver.find_element_by_xpath('//li/a[@href="/cate/python/"]')
    12 #鼠标先移动到“编程语言”上,然后点击Python
    13 ActionChains(driver).move_to_element(program_lan).click(program_py).perform()
    14 
    15 driver.quit()

    (五) 查看Python selenium API的说明文档

    点击下面的这个(或者也可以通过命令启动python -m pydoc -p 3333 ,-p指端口号(自定义一个)),然后在浏览器登录后(http://localhost:3333/),输入selenium进行查询

  • 相关阅读:
    【转】 java中Class对象详解和类名.class, class.forName(), getClass()区别
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    107. Binary Tree Level Order Traversal II
    109. Convert Sorted List to Binary Search Tree
    108. Convert Sorted Array to Binary Search Tree
    110. Balanced Binary Tree
    STL容器迭代器失效问题讨论
    113. Path Sum II
    112. Path Sum
  • 原文地址:https://www.cnblogs.com/simple-free/p/8527246.html
Copyright © 2011-2022 走看看