轻松自动化---selenium-webdriver(python) (四)

http://www.testclass.net/  测试教程网,专业的selenium 学习网站。

本节要解决的问题:

如何定位一组元素?

场景

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

这时候就需要使用findElements方法。

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

· 批量操作对象,比如将页面上所有的checkbox都勾上

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

<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>Checkbox</title>
        <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
        <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    </head>
    <body>
        <h3>checkbox</h3>
        <div class="well">
            <form class="form-horizontal">
                <div class="control-group">
                    <label class="control-label" for="c1">checkbox1</label>
                    <div class="controls">
                        <input type="checkbox" id="c1" />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="c2">checkbox2</label>
                    <div class="controls">
                        <input type="checkbox" id="c2" />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="c3">checkbox3</label>
                    <div class="controls">
                        <input type="checkbox" id="c3" />
                    </div>
                </div>    

                <div class="control-group">
                    <label class="control-label" for="r">radio</label>
                    <div class="controls">
                        <input type="radio" id="r1" />
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="r">radio</label>
                    <div class="controls">
                        <input type="radio" id="r2" />
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

将这段代码保存复制到记事本中,将保存成checkbox.html文件。(注意,这个页面需要和我们的自动化脚本放在同一个目录下)

第一种方法:

通过浏览器打个这个页面我们看到三个复选框和两个单选框。下面我们就来定位这三个复选框。

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import os

dr = webdriver.Firefox()
file_path =  ‘file:///‘ + os.path.abspath(‘checkbox.html‘)
dr.get(file_path)

# 选择页面上所有的input,然后从中过滤出所有的checkbox并勾选之
inputs = dr.find_elements_by_tag_name(‘input‘)
for input in inputs:
    if input.get_attribute(‘type‘) == ‘checkbox‘:
        input.click()
time.sleep(2)

dr.quit()

你可以试着把input.get_attribute(‘type‘) == ‘checkbox‘ 中的checkbox 变成radio ,那这个脚本定位的会是两个单选框。

第二种定位方法:

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import os

dr = webdriver.Firefox()
file_path =  ‘file:///‘ + os.path.abspath(‘checkbox.html‘)
dr.get(file_path)

# 选择所有的checkbox并全部勾上
checkboxes = dr.find_elements_by_css_selector(‘input[type=checkbox]‘)
for checkbox in checkboxes:
    checkbox.click()
time.sleep(2)

# 打印当前页面上有多少个checkbox
print len(dr.find_elements_by_css_selector(‘input[type=checkbox]‘))
time.sleep(2)

dr.quit()

第二种写法与第一种写法差别不大,都是通过一个循环来勾选控件;如果你学过上一章的话,细心的你一定发现用的定位函数不一样,

第一种用的name ,第二种用的CSS 。

 如何去掉勾选:

还有一个问题,有时候我们并不想勾选页面的所有的复选框(checkbox),可以通过下面办法把最后一个被勾选的框去掉。如下:

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import os

dr = webdriver.Firefox()
file_path =  ‘file:///‘ + os.path.abspath(‘checkbox.html‘)
dr.get(file_path)

# 选择所有的checkbox并全部勾上
checkboxes = dr.find_elements_by_css_selector(‘input[type=checkbox]‘)
for checkbox in checkboxes:
    checkbox.click()
time.sleep(2)

# 把页面上最后1个checkbox的勾给去掉
dr.find_elements_by_css_selector(‘input[type=checkbox]‘).pop().click()
time.sleep(2)

dr.quit()

其实,去掉勾选表也逻辑也非常简单,就是再次点击勾选的按钮。可能我们比较迷惑的是如何找到“最后一个”按钮。pop() 可以实现这个功能。

好吧!在web自动化的学习过程中,我们必须要知道一些前端的东西,这里扩展一下:

http://www.w3school.com.cn/js/jsref_pop.asp

尝试

把find_elements_by_css_selector(‘input[type=checkbox]‘).pop().click() 中的checkbox 变成radio 会是什么效果,自己尝试一下吧!

--------------------------

学习更多selenium 内容:

「功能测试自动化」汇总

原文地址:https://www.cnblogs.com/Raul2018/p/9359032.html

时间: 2024-08-05 09:07:12

轻松自动化---selenium-webdriver(python) (四)的相关文章

Selenium WebDriver + Python 环境配置

1.   下载必要工具及安装包 1.1.[Python开发环境] 下载并安装Python 2.7.x版本(当前支持2.x版本,不要下载最新的3.X的版本因为python3并非完全兼容python2) 下载地址:https://www.python.org/downloads/ 1.2.[python 的安装包管理工具]Pip pip 是python 软件包的安装和管理工具,有了这个工具,我们只需要一个命令就可以轻松的python 的任意类库. 下载地址: https://pypi.python.

【转】Selenium WebDriver + Python 环境

转自:http://www.myext.cn/webkf/a_11878.html 1. 下载必要工具及安装包 1.1 [Python开发环境] 下载并安装Python 2.7.x版本 下载地址:https://www.python.org/downloads/ 1.2 [python 的安装包管理工具]Pip pip 是python 软件包的安装和管理工具,有了这个工具,我们只需要一个命令就可以轻松的python的任意类库. 下载地址:https://pypi.python.org/pypi/

selenium webdriver (python)大全

webdriver的简介 硒2.0的主要新功能是集成的webdriver的API.webdriver的设计除了解决一些seleniumr-RC API的一些限制,与webdriver 的整合,将提供一个更简单,更简洁的编程接口.selenium webdriver会更好地支持动态的网页,页面本身被重新加载页面元素可能更改.webdriver的目标是提供一个设计良好的面向对象的API,提供了更好的支持现代先进的web-app测试. WebDriver与Selenium-RC相比,是如何来驱动浏览器

selenium webdriver python 开始

学习资料: Selenium with Python: http://selenium-python.readthedocs.org/en/latest/index.html Selenium WebDriver主要用来写 functional/acceptance tests. 当前支持Python版本: 2.7,3.2,3.3,3.4 当前支持的浏览器: Firefox, Chrome, Ie and Remote

Selenium WebDriver +Python讲解

1. Selenium1.0家谱: 1.1 Selenium IDE :是嵌入到浏览器中的一个插件,实现简单的浏览器操作的录制和回放功能.官方给出的定义:快速的创建bug重现脚本,在测试人员测试过程中,发现bug之后可以通过IDE将重新的步骤录制下来,以帮助开发人员更容易的重现bug. 1.2 Selenium Grid:是一种自动化的测试辅助工具,Grid通过利用现有的计算机基础设施,能加快Web-App的功能测试.利用Grid可以很方便地实现多台机器上和已购环境中运行测试用例. 1.3 Se

selenium webdriver python 环境搭建

1. 安装python https://www.python.org/getit/ 选择2.7版本,下载安装即可. 验证是否安装成功:打开cmd,输入"python -V",显示python版本号即安装成功. 2. 安装setuptools https://pypi.python.org/pypi/setuptools 安装步骤 下载setuptools-18.5.zip,完成之后解压 打开cmd,进入路径setuptools-18.5 python setup.py install

selenium webdriver+python基本操作

导入模块: from selenium import webdriver from selenium.common.exceptions import NoSuchElementException 选择浏览器: driver = webdriver.Firefox() 打开url: driver.get("http://www.baidu.com") 等待: driver.implicitly_wait(30) driver.set_page_load_timeout(30) driv

selenium+webdriver+python 中警告框的处理方法

在自动化测试过程中,经常会遇到弹出警告框的情况,如图所示: 在 WebDriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体做法是使用 switch_to_alert()方法定位到 alert/confirm/prompt.然后使用 text/accept/dismiss/send_keys 按需进行操做.1. 获取警告框的text消息 2. 接受消息框(确定) 3. 取消 4. 输入值 text 返回 alert/confirm/

转:selenium webdriver+python基本操作

转自: http://blog.163.com/[email protected]/blog/static/1017337222013102310617946/ 导入模块: from selenium import webdriver from selenium.common.exceptions import NoSuchElementException 选择浏览器: driver = webdriver.Firefox() 打开url: driver.get("http://www.baid

selenium webdriver python 操作浏览器

新建driver driver=webdriver.Firefox() driver=webdriver.Ie() driver=webdriver.Chrome() 改变浏览器 将浏览器最大化 driver.maximize_window() 设置浏览器大小 driver.set_window_size(480,800) 浏览器前进.后退 driver.forward() driver.back() 设置浏览器位置 driver. set_window_position(0,0) 关闭浏览器