zoukankan      html  css  js  c++  java
  • Python+Selenium学习--定位一组对象

    场景

    从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法。

    定位一组对象一般用于以下场景:

    • 批量操作对象,比如将页面上所有的checkbox都勾上
    • 先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的checkbox,然后选择最后一个

    代码

    S6.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form enctype="multipart/form-data">
            <div>
                <input type="text" name="user" />
                <p>请选择性别:</p>
                男:<input type="radio" name="gender" value="1"  />
                女:<input type="radio" name="gender" value="2" checked="checked"/>
                Alex:<input type="radio" name="gender" value="3"/>
                <p>爱好</p>
                篮球:<input type="checkbox" name="favor"  value="1" />
                足球:<input type="checkbox" name="favor"  value="2" checked="checked" />
                皮球:<input type="checkbox" name="favor"  value="3" />
                台球:<input type="checkbox" name="favor"  value="4" checked="checked"/>
                网球:<input type="checkbox" name="favor"  value="5" />
                <p>上传文件</p>
                <input type="file" name="fname"/>
            </div>
    
            <input type="submit" value="提交" />
            <input type="reset" value="重置" />
        </form>
    </body>
    </html>
    

      定位一组对象.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    '''
    Created on 2018/5/9 11:35
    @author: Jeff Lee
    @file: 定位一组对象.py
    '''
    from selenium import webdriver
    from time import sleep
    import os
    
    if'HTTP_PROXY'in os.environ:
        del os.environ['HTTP_PROXY']
    
    dr = webdriver.Firefox()
    file_path ='file://'+ os.path.abspath('s6.html')
    print (file_path)
    dr.get(file_path)
    
    ## 选择所有的checkbox并全部勾上
    checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
    for checkbox in checkboxes:
        if not checkbox.get_attribute('checked'):
            checkbox.click()
    sleep(1)
    dr.refresh()
    sleep(2)
    
    # 打印当前页面上有多少个checkbox
    print (len(dr.find_elements_by_css_selector('input[type=checkbox]')))
    
    # 选择页面上所有的input,然后从中过滤出所有的checkbox并勾选之
    inputs = dr.find_elements_by_tag_name('input')
    for input in inputs:
            if input.get_attribute('type') == 'checkbox':
                if not input.get_attribute('checked'):
                    input.click()
    sleep(1)
    
    # 把页面上最后1个checkbox的勾给去掉
    dr.find_elements_by_css_selector('input[type=checkbox]').pop().click()
    sleep(1)
    
    dr.quit()
    

      

  • 相关阅读:
    python中以带mixin命名的类有什么特点?
    php使用redis做缓存和使用redis保存session
    python连接hive数据库count查询慢的解决办法
    内网环境数据库查看工具使用笔记支持hive edismysql
    深入mysql的视图复习笔记
    Laravel 整合IOS苹果授权登录(JWT验证模式)
    PHP 读取PDF文件内容之PdfParser
    git发生冲突:error: Your local changes to the following files would be overwritten by merge
    PHP 创建GUID唯一标识
    Laravel 模型关联、关联查询、预加载使用实例
  • 原文地址:https://www.cnblogs.com/uniquefu/p/9018190.html
Copyright © 2011-2022 走看看