zoukankan      html  css  js  c++  java
  • 爬虫之selenium和webdriver—基础(三):行为链

    有时候在页面的操作很多,那么这时候可以使用行为链类ActionChains类完成。比如 现在要将鼠标移动到某个元素上并执行点击事件。

     1 from selenium import webdriver
     2 from selenium.webdriver.common.action_chains import ActionChains
     3 driver_path = 'D:chromedriverchromedriver.exe'
     4 driver = webdriver.Chrome(executable_path=driver_path)
     5 driver.get('https://www.baidu.com')
     6 
     7 
     8
     9 inputTag = driver.find_element_by_id('kw') #输入框
    10 submitTag = driver.find_element_by_id('su') #button按钮
    11 actions = ActionChains(driver)
    12 actions.move_to_element(inputTag) #把鼠标移动到输入框上
    13 actions.send_keys_to_element(inputTag, 'python') #在输入框输入python
    14 actions.move_to_element(submitTag) #将鼠标移动到button按钮上
    15 actions.click(submitTag) #点击
    16 actions.perform() #将上面所有的行为串联起来

    还可以写在一起:ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

    一些常见的的鼠标操作:

    click_and_hold(element):点击但不松开鼠标

    context_click(element):右键点击

    double_click(element):双击

  • 相关阅读:
    Qt状态机实例
    <STL> accumulate 与 自定义数据类型
    <STL> 容器混合使用
    散列表(C版)
    Canonical 要将 Qt 应用带入 Ubuntu
    <STL> set随笔
    C++ 文件流
    视频播放的基本原理
    <STL> pair随笔
    c++ 内存存储 解决char*p, char p[]的问题
  • 原文地址:https://www.cnblogs.com/GouQ/p/13093339.html
Copyright © 2011-2022 走看看