zoukankan      html  css  js  c++  java
  • Selenium(十):用By定位元素、鼠标事件、键盘事件

    1. 用By定位元素

    除了前面介绍的单位方法,WebDriver还提供了另外一套写法,即统一调用find_element()方法,通过By来声明定位的方法,并且传入对应定位方法的定位参数。具体如下:

    from time import sleep
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    wd = webdriver.Chrome()
    
    wd.get('https://www.baidu.com/')
    
    wd.find_element(By.ID,"kw")
    wd.find_element(By.NAME,"wd")
    wd.find_element(By.CLASS_NAME,"s_ipt")
    wd.find_element(By.TAG_NAME,"input")
    wd.find_element(By.LINK_TEXT,u"新闻")
    wd.find_element(By.PARTIAL_LINK_TEXT,u"")
    wd.find_element(By.XPATH,"//*[@class='bg s_btn']")
    wd.find_element(By.CSS_SELECTOR,"span.bg.s_btn_wr>input#su")

    find_element()方法只用于定位元素。它需要两个参数,第一个参数是定位的类型,由By提供:第二个参数是定位的具体方式。在使用By之前需要将By类导入。

    from selenium.webdriver.common.by import By

    通过查看WebDriver的底层实现代码发现它们其实是一回事儿,例如,find_element_by_id()方法的实现。

    def find_element_by_id(self, id_):
        """Finds an element by id.
    
        :Args:
        - id\_ - The id of the element to be found.
    
        :Returns:
        - WebElement - the element if it was found
    
        :Raises:
        - NoSuchElementException - if the element wasn't found
    
        :Usage:
            element = driver.find_element_by_id('foo')
        """
        return self.find_element(by=By.ID, value=id_)

    但WebDriver更推荐前面介绍的写法,当然我公司底层是使用By来分装函数的。

    2. 鼠标事件

    前面不但使用过click()方法,而且还了解了下其他鼠标交互方式,例如鼠标点击、双击、悬停、甚至是鼠标拖动等功能。

    在这一章中,我将详细说明这些功能。

    在WebDriver中,将这些关于鼠标操作的方法封装在ActionChains类提供。

    ActionChains类提供了鼠标操作的常用方法:

    perform():执行所有ActionChains中存储的行为

    context_click():右击

    double_click():双击

    drag_and_drop():拖动

    move_to_element():鼠标悬停

    2.1 鼠标右击操作

    对于ActionChains类所提供的鼠标方法与前面学过的click()方法的用法有所不同。

    我想了半天,愣是没想到哪个网站可以右键,所以没办法弄出实例了。

    导入提供鼠标操作的ActionChains:

    from selenium.webdriver import ActionChains

    调用ActionChains(driver)类,将浏览器驱动driver作为参数传入,我一般是用wd作为浏览器驱动的名称。

    ActionChains(wd)

    context_click(right)方法用于秒你鼠标右键操作,在调用是需要制定元素定位。

    ActionChains(wd).context_click(right)

    执行所有ActionChains中存储的行为,可以理解成是对整个操作的提交动作。

    ActionChains(wd).context_click(right).perform()

    2.2 鼠标悬停

    鼠标悬停弹出下拉菜单是一个十分创建的功能设计。

    move_to_element()方法可以模拟鼠标悬停的动作,其用法与context_click()相同。

    from time import sleep
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    wd = webdriver.Chrome()
    
    wd.get('https://www.baidu.com/')
    
    #定位到要鼠标悬停的元素
    above = wd.find_element_by_xpath("//div[@id='u1']/a[8]")
    #对定位到的元素执行鼠标悬停操作
    ActionChains(wd).move_to_element(above).perform()

    2.3 鼠标双击操作

    double_click方法用于模拟鼠标双击操作。和前面的使用方法一模一样,就不用代码演示了。

    2.4 鼠标拖放操作

    drag_and_drop(source,target)在源元素上按住鼠标左键,然后移动到目标元素上释放。

    source:鼠标拖动的源元素

    target:鼠标释放的目标元素

    这个也没想到哪里可以使用到,可能后面的滑动式验证码需要使用。

    3. 键盘事件

    Keys()类提供了键盘上几乎所有按键的方法。前面了解到,send_keys()方法可以用来模拟键盘输入,除此之外,我们还可以用它来输入键盘上的按键,甚至是组合键,如Ctrl+A、Ctrl+C等。

    from time import sleep
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    wd = webdriver.Chrome()
    
    wd.get('https://www.baidu.com/')
    
    #输入框输入内容
    wd.find_element_by_id("kw").send_keys("seleniumm")
    
    #删除多输入的一个m
    wd.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
    
    #输入空格键+“教程”
    wd.find_element_by_id("kw").send_keys(Keys.SPACE)
    wd.find_element_by_id("kw").send_keys("教程")
    
    #Ctrl+a 全选输入框内容
    wd.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
    
    #Ctrl+x 剪切输入框内容
    wd.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
    
    #Ctrl+v 粘贴输入框内容
    wd.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')
    
    #通过回车键来代替单击操作
    wd.find_element_by_id("su").send_keys(Keys.ENTER)

    需要说明的是,上面的代码没有什么实际意义,仅向我们展示模拟键盘各种按键与组合键的使用。

    在使用键盘按键方法前需要先导入keys类。

    from selenium.webdriver.common.keys import Keys

    以下是常用的键盘操作:

    send_keys(Keys.BACK_SPACE)    删除键

    send_keys(Keys.SPACE)    空格键

    send_keys(Keys.TAB)    制表键

    send_keys(Keys.ESCAPE)  回退键

    send_keys(Keys.ENTER)  回车键

    send_keys(Keys.CONTROL,'a')  全选

    send_keys(Keys.CONTROL,'c')  复制

    send_keys(Keys.CONTROL,'x')   剪切

    send_keys(Keys.CONTROL,'v')   粘贴

    send_keys(Keys.F1)  键盘F1

    ......

    send_keys(Keys.F12)  键盘F12

  • 相关阅读:
    oracle 导入数据时提示只有 DBA 才能导入由其他 DBA 导出的文件
    oracle 常用语句
    android udp 无法收到数据 (模拟器中)
    android DatagramSocket send 发送数据出错
    AtCoder ABC 128E Roadwork
    AtCoder ABC 128D equeue
    AtCoder ABC 127F Absolute Minima
    AtCoder ABC 127E Cell Distance
    CodeForces 1166E The LCMs Must be Large
    CodeForces 1166D Cute Sequences
  • 原文地址:https://www.cnblogs.com/liuhui0308/p/11960688.html
Copyright © 2011-2022 走看看