selenium webdriver

一、定位一组元素:

webdriver可以很方便的使用findElement 方法来定位某个物定的对象。不过有时候我们却要定位一组对象,这时候就需要使用findElements方法。

应用场景:

1. 批量操作对象,比如将页面上所有checkbox都选择上;

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

二、多层框架、窗口定位

switch_to_frame()

switch_to_window()

1. 多层框架定位 

有时候我们定位一个元素,定位器没有问题,但一直定位不了,这时候要检查这个元素是否在一个frame中,selenium webdriver提供了一个switch_to_frame方法,可以很轻松的解决这个问题。

示例:

from selenium import webdriver

import time

import os

browser = webdrivers.Firefox()

file_path = 'file:///' + os.path.abspath('frame.html')

browser.get(file_path)

time.sleep(10)

#先找到frame文件

browser.switch_to_frame("f1")

browser.switch_to_frame("f2")

#然后就可以正常的操作元素了

browser.find.element_by_id("kw").send_keys("Python")

browser.find.element_by_id("su").click()

time.sleep(3)

browser.quit()

2. 多层窗口定位

有可能嵌套的不是框架,而是窗口,还有针对窗口的方法:

switch_to_window方法与switch_to_frame相同

示例:

driver.switch_to_window("windowName")

三、层级定位

在实际的测试中,可能会遇到这种问题:页面上有很多个属性基本相同的元素,现在家需要具体定到其中的一个。由于发生基本相当,所以在定位的时候会有些麻烦,这个时候就需要用到层级定位。先定位到父元素,然后再通过父元素定位到子元素。

from selenium.webdriver.supper.ui import WebDriverWait

webDriverWait(dr,10)    #10s内每隔500ms扫描一次页面变化,当出现指定的元素后结束。

is_displayed()        #该元素量是否用户可以见

class ActionChains(driver)  #生成用户的行为。

move_to_element(menu)    #移动鼠标到一个元素中,menu上面已经定义了它所指向的哪一个元素。

perform()            #执行所有点存储的行为。

selenium webdriver 学习笔记(二)的更多相关文章

  1. selenium webdriver学习(二)————对浏览器的简单操作(转载JARVI)

    selenium webdriver学习(二)————对浏览器的简单操作 博客分类: Selenium-webdriver   selenium webdriver对浏览器的简单操作 打开一个测试浏览 ...

  2. selenium webdriver 学习笔记(三)

    selenium webdriver 一.上传文件操作 上传文件夹一般要打开一个本地窗口,从窗口选择本地文件添加.所以一般会卡在如何操作本地窗口添加上传文件. 其实,在selenium webdriv ...

  3. selenium webdriver 学习笔记(一)

    selenium webdriver 第一个脚本: #coding = utf-8 from selenium import webdriver import time url = "htt ...

  4. Selenium WebDriver 学习笔记

    1. 打开VS2012 2. 新建工程(单元测试工程或控制台程序都可以, 看需求) 3. 工具->NuGet程序包管理器->程序包管理器控制台 4. 输入"Install-Pac ...

  5. Selenium webdriver 学习总结-元素定位

    Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...

  6. Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )

    !/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...

  7. Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...

  8. Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...

  9. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

随机推荐

  1. poj2752Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  2. Netty详解

    Netty详解  http://blog.csdn.net/suifeng3051/article/category/2161821

  3. 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library

    1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...

  4. DataGridview 填写数字

    private DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit        private void dgv ...

  5. PSR-2 Coding Style Guide

    本文主要是对PSR-2 的简单翻译. 英文源址 http://www.php-fig.org/psr/psr-2/ PSR2继承和扩展PSR1--基本编码规范 本手册的目的是使用一系列共同遵守的编码格 ...

  6. 关于python命令在editor里编写与在interpreter里的编写的不同之处

    关于python命令在editor里编写与在interpreter里的编写的不同之处 其实用这个标题,我心里还是有点胆怯的.作为一个python入门的小白,不,编程入门的小白,我还不太确定我对edit ...

  7. python---连接MySQL第三页

    用python语言从MySQL中查询数据 #!conding:utf-8 from mysql.connector import errorcode import mysql.connector cn ...

  8. android模块

    网络模块 1.URL --------openStream() return InputStream --------openConnection() return URLConnection 2.U ...

  9. Mirantis Fuel fundations

    Mirantis Nailgun is the most important service a RESTful application written in Python that contains ...

  10. kubuntu添加windows字体

    1.选取字体 在/usr/share/fonts/truetype中新建一个目录,命名为ms,然后将Windows\fonts 目录下的tahoma.ttf.tahomabd.ttf(Tahoma的粗 ...