• web自动化中的鼠标操作


    selenium中的ActionChains类用来完成模拟鼠标操作

    主要流程:

      1、存储鼠标的操作

      2、perform()来执行鼠标操作

    1、鼠标的悬停操作

     1 from selenium import webdriver
     2 from selenium.webdriver.common.action_chains import ActionChains
     3 driver=webdriver.Chrome()
     4 driver.maximize_window()
     5 # 访问一个网页
     6 driver.get("https://www.baidu.com")
     7 
     8 # 1、先找到鼠标要操作的元素
     9 ele=driver.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_settingicon"]')
    10 
    11 # 2、实例化ActionChains类
    12 ac=ActionChains(driver)
    13 # 3、将鼠标操作添加到actions列表中
    14 ac.move_to_element(ele)
    15 # 4、调用perform()函数来执行鼠标操作
    16 ac.perform()

      上面的鼠标操作可以简化成一步来完成,应为函数的返回值都是实例本身(self)所以可以直接调用,详细可以操作ActionChains类中的函数

      ActionChains(driver).move_to_element(ele).perform()

    • 对于鼠标悬停后出现的元素进行定位

       将鼠标的焦点停留到按F12出现的element区域,按住crl+shift+c不要松开,鼠标左键放到html页面需要定位元素位置,松开crl+shift+c,点击定位的元素

    2、处理鼠标悬停出现的下拉列表  

     1 from selenium import webdriver
     2 from selenium.webdriver.common.action_chains import ActionChains
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.support import expected_conditions as EC
     5 from selenium.webdriver.common.by import By
     6 driver=webdriver.Chrome()
     7 driver.maximize_window()
     8 # 访问一个网页
     9 driver.get("https://www.baidu.com")
    10 # 1、先找到鼠标要操作的元素
    11 ele=driver.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_settingicon"]')
    12 ActionChains(driver).move_to_element(ele).perform()
    13 # 连续操作,一行代码实现鼠标操作,因为函数返回的都是实例的本身(self)
    14 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//a[text()='高级搜索']")))
    15 driver.find_element_by_xpath("//a[text()='高级搜索']").click()

    3、select下拉列表使用Select类来处理

    选择下拉列表的值:
    1、通过下标选择:select_by_index(index)从0开始
    2、通过value属性:select_by_value(value值)
    3、通过文本内容:select_by_visible_text(文本内容)
    代码实现如下:
     1 from selenium import webdriver
     2 from selenium.webdriver.common.action_chains import ActionChains
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.support import expected_conditions as EC
     5 from selenium.webdriver.common.by import By
     6 from selenium.webdriver.support.select import Select
     7 driver=webdriver.Chrome()
     8 driver.maximize_window()
     9 # 访问一个网页
    10 driver.get("https://www.baidu.com")
    11 # 1、先找到鼠标要操作的元素
    12 ele=driver.find_element_by_xpath("//div[@id='u1']//*[@name='tj_settingicon' and text()='设置']")
    13 ActionChains(driver).move_to_element(ele).perform()
    14 # 连续操作,一行代码实现鼠标操作,因为函数返回的都是实例的本身(self)
    15 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//a[text()='高级搜索']")))
    16 driver.find_element_by_xpath("//a[text()='高级搜索']").click()
    17 
    18 # 2、实例化ActionChains类
    19 # ac=ActionChains(driver)
    20 # # 3、将鼠标操作添加到actions列表中
    21 # ac.move_to_element(ele)
    22 # # 4、调用perform()函数来执行鼠标操作
    23 # ac.perform()
    24 # 使用selenium提供的Select类来处理select/option下拉列表
    25 '''
    26  选择下拉列表的值:
    27     1、通过下标选择:select_by_index(index)从0开始
    28     2、通过value属性:select_by_value(value值)
    29     3、通过文本内容:select_by_visible_text(文本内容)
    30 '''
    31 # 1、找到select元素
    32 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.NAME,'ft')))
    33 select_ele=driver.find_element_by_name('ft')
    34 # 2、实例化Select类
    35 s=Select(select_ele)
    36 # 3、选择下拉列表值
    37 # 方式一:下表  从0开始
    38 s.select_by_index(2)
    39 # 方式二:value值
    40 s.select_by_value('ppt')
    41 # 方式三:文本内容
    42 s.select_by_visible_text('RTF 文件 (.rtf)')
  • 相关阅读:
    POJ3984-迷宫问题【BFS】
    BFS与DFS模板
    nyoj27-水池数目【DFS】
    C++ STL-stack使用详解
    C++ STL
    HDU1058
    HDU1114
    HDU1867
    Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner
    Codeforces Round #461 (Div. 2) C. Cave Painting
  • 原文地址:https://www.cnblogs.com/wsk1988/p/12699230.html
Copyright © 2020-2023  润新知