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):双击

  • 相关阅读:
    CSS选择器
    CSS选择器详解(二)通用选择器和高级选择器
    CSS选择器详解(一)常用选择器
    30个最常用css选择器解析
    常用CSS缩写语法总结
    XHTML 代码规范
    命名空间(xmlns属性)
    HTML 5 <meta> 标签
    HTML <!DOCTYPE> 标签
    Mybatis-generator 逆向工程
  • 原文地址:https://www.cnblogs.com/GouQ/p/13093339.html
Copyright © 2011-2022 走看看