zoukankan      html  css  js  c++  java
  • 爬虫之selenium和webdriver—基础(二):定位元素的方法和操作表单元素

    一、常见的定位元素方法

     1 from selenium import webdriver
     2 from selenium.webdriver.common.by import By 
     3 driver_path = 'D:chromedriverchromedriver.exe'
     4 driver = webdriver.Chrome(executable_path=driver_path)
     5 driver.get('https://www.baidu.com')
     6 
     7 #1、find_element_by_id.下面两种方法等价
     8 inputTag = driver.find_element_by_id('kw')
     9 inputTag = driver.find_element(By.ID,'kw')
    10 inputTag.send_keys('python')
    11 
    12 #2、find_element_by_class_name.下面两种方法等价
    13 submitTag = driver.find_element_by_class_name('fg')
    14 submitTag = driver.find_element(By.CLASS_NAME,'fg')
    15 
    16 #3、driver.find_element_by_name.下面两种方法等价
    17 submitTag = driver.find_element_by_name('fg')
    18 submitTag = driver.find_element(By.NAME,'fg')
    19 
    20 #4、find_element_by_tag_name.下面两种方法等价
    21 submitTag = driver.find_element_by_tag_name('fg')
    22 submitTag = driver.find_element(By.TAG_NAME,'fg')
    23 
    24 #5、find_element_by_xpath.下面两种方法等价
    25 submitTag = driver.find_element_by_xpath('//div/a')
    26 submitTag = driver.find_element(By.XPATH,'//div/a')
    27 
    28 #6、find_element_css_selector.下面两种方法等价
    29 submitTag = driver.find_element_by_css_selector('.quickdelete-wrap > input')
    30 submitTag = driver.find_element(By.CSS_SELECTOR,'.quickdelete-wrap > input')
    31 
    32 #find_element是获取第一个满足条件的元素,find_elements是获取所有满足条件的元素

    二、操作表单元素

    常见的表单元素

    1、input:一般type='text/password/email/number'

    操作分为两步。第一步找到这个元素,第二步使用send_keys(value),将数据填充进去

    1 inputTag = driver.find_element_by_id('kw')
    2 inputTag.send_keys('python')
    3 inputTag.clear()

    使用clear方法可以清楚输入矿中的内容

    2、button:一般type='submit'

    操作按钮有很多种方式,比如单击、右击、双击等。最常用的就是点击,直接调用click函数就可以。

    1 inputTag = driver.find_element_by_id('kw') #输入框
    2 inputTag.send_keys('python')
    3 
    4 submitTag = driver.find_element_by_id('su') #点击按钮
    5 submitTag.click()

    3、checkbox:一般type='checkbox'

    1 remember = driver.find_element_by_name('remember')
    2 remember.click()  #将checkbox框打钩
    3 remember.click()  #将checkbox框取消打钩

    4、select:一般为下拉列表,select标签

    select元素不能直接点击,因为点击还需要选中元素。这时候selenium就专门为select标签提供了一个类selenium.webdriver.support.ui.select。将获取到的元素当成参数传到这个类中,创建这个对象。以后就可以使用这个对象进行选择了。

     示例代码

     1 from selenium import webdriver
     2 from selenium.webdriver.support.ui import Select
     3 driver_path = 'D:chromedriverchromedriver.exe'
     4 driver = webdriver.Chrome(executable_path=driver_path)
     5 driver.get('https://www.youdao.com')
     6 
     7 selectBtn = Select(driver.find_element_by_name('JumpMenu'))
     8 #以下三种方法等价
     9 selectBtn.select_by_index(1)
    10 selectBtn.select_by_value('http://m.dsf.com/')
    11 selectBtn.select_by_visible_text('中法')
    12 selectBtn.deselect_all() #取消选中的所有选项
  • 相关阅读:
    is quoted with ["] which must be escaped when used within the value
    QueryDSL与SpringDataJPA复杂查询
    遍历list,同时remove不符合条件的元素
    解决AnnotationTransactionAttributeSource is only available on Java 1.5 and highe
    Windows系统安装MySQL
    sqlyog导sql文件
    myeclipse导入maven项目
    Invalid 'log4jConfigLocation' parameter: class path resource [log4j.xml] cannot be resolved to URL because it does not exist
    Nginx SSL+tomcat集群,取不到https正确协议
    微信开发之通过代理调试本地项目
  • 原文地址:https://www.cnblogs.com/GouQ/p/13093194.html
Copyright © 2011-2022 走看看