<译>Selenium Python Bindings 3 - Navigating
当你想要通过webdriver导航到一个链接,正常的方式点是通过调用get方法:
driver.get("http://www.google.com")
- Interacting with the page
在页面中的HTML元素。如果我们需要找到定位一个。那么webdriver提供了许多方法来寻找元素。例如给了一个HTML的标签:<input type="text" name="passwd" id="passwd-id" />
你可以使用下面任一方法定位:
element = driver.find_element_by_id("passwd-id") element = driver.find_element_by_name("passwd") element = driver.find_element_by_xpath("//input[@id='passwd-id']")你也可以通过文本来定位链接,但要小心!文字必须是完全匹配!在webdriver使用 XPATH时也要小心。如果有符合的查询多个元素,则只有第一个将被退回。如果没有,一个NoSuchElementException异常会引发
webdriver拥有基于对象的API;我们使用相同接口代表各种类型元素。这意味着,虽然你可能会看到很多你可以引用的方法,当你打你的IDE的自动完成组合键,不是所有的都是有意义或有效。但webdriver将尝试做正确的事情。
所以,如果你已经定位一个元素,我们能对它做什么处理呢?首先,你可以输入一些文本内容在text field:element.send_keys("some text")你能够通过“Keys”类来模拟方向键输入:
element.send_keys(" and some", Keys.ARROW_DOWN)你也可以使用clear方法清除输入的内容:
element.clear()
- Filling in forms
我们已经看到了如何将文本输入到一个文本域或文本字段,但对于其他元素?您可以“切换”下拉状态,可以使用“setSelected”设置选择的选项标签。element = driver.find_element_by_xpath("//select[@name='name']") all_options = element.find_elements_by_tag_name("option") for option in all_options: print("Value is: %s" % option.get_attribute("value")) option.click()这将找到页面上的第一个“SELECT”元素,并依次循环每一个OPTION,打印出它们的值,并依次选择每一个。
正如你所看到的,这不是最有效处理SELECT元素的方式。WebDriver支持的类包括一个名为“Select”的类,它提供了与这些交互的有用的方法:
from selenium.webdriver.support.ui import Select select = Select(driver.find_element_by_name('name')) select.select_by_index(index) select.select_by_visible_text("text") select.select_by_value(value)WebDriver也提供了反选options的功能:
select = Select(driver.find_element_by_id('id')) select.deselect_all()假设在一个测试中,我们需要列出所有默认选择的选项,Select类提供了合理的方法,返回一个列表:
select = Select(driver.find_element_by_xpath("xpath")) all_selected_options = select.all_selected_options为了获取所有可用的options:
options = select.options
一旦你完成了所有表单的填写,你可能会提交它:
# Assume the button has the ID "submit" :) driver.find_element_by_id("submit").click() - Drag and drop
您可以使用拖放,或者通过确定的范围移动元素,或到另一个元素:element = driver.find_element_by_name("source") target = driver.find_element_by_name("target") from selenium.webdriver import ActionChains action_chains = ActionChains(driver) action_chains.drag_and_drop(element, target) - Moving between windows and frames
Webdriver支持使用“switch_to_window”方法来在窗口间移动:driver.switch_to_window("windowName")所有调用驱动现在将被解释为被引导到特定的窗口。但你怎么知道窗口的名字?看一看在打开它的JavaScript或链接:
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
或者,您也可以通过一个“window handle”到“switch_to_window()”方法。知道了这一点,它可以遍历每个打开的窗口,如下所示:
for handle in driver.window_handles: driver.switch_to_window(handle)你也可以摆动一个框架向另一个框架(或向一个框架里):
driver.switch_to_frame("frameName")它可以通过以点来分隔路径,访问子帧,并且可以制定框架通过它的索引。例如:
driver.switch_to_frame("frameName.0.child")当所有关于框架的操作完成后,我们需要回到父框架:
driver.switch_to_default_content()
- Popup dialogs
Selenium webdriver 内置了用于处理弹出对话框的支持。当你点击,将打开一个弹出窗口中,您可以访问警报,执行以下操作:
alert = driver.switch_to_alert()
这将返回当前打开的警示对象。有了这个对象,你现在可以接受,拒绝,阅读其内容,甚至键入一个提示。这个接口同样适用于警示,确认,提示。
- Navigation : history and location
driver.forward() driver.back()
- Cookies
在我们退出这些后续步骤中,您可能有兴趣了解如何使用cookie。首先,你需要在域名内,以至于cookie将有效:# Go to the correct domain driver.get("http://www.example.com") # Now set the cookie. This one's valid for the entire domain cookie = {"key": "value"} driver.add_cookie(cookie) # And now output all the available cookies for the current URL all_cookies = driver.get_cookies() for cookie_name, cookie_value in all_cookies.items(): print("%s -> %s", cookie_name, cookie_value)
<译>Selenium Python Bindings 3 - Navigating的更多相关文章
- <译>Selenium Python Bindings 1 - Installation
Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...
- <译>Selenium Python Bindings 5 - Waits
如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...
- <译>Selenium Python Bindings 2 - Getting Started
Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...
- <译>Selenium Python Bindings 6 - WebDriver API
本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...
- <译>Selenium Python Bindings 4 - Locating Eelements
有各种不同的策略来定位页面中的元素.你可以使用最合适定位方式用于你的用例.Selenium提供了以下方法来定位页面中的元素: find_element_by_id find_element_by_na ...
- selenium python bindings 元素定位
1. 辅助 Firepath Firefox是所有做前端的必不可少的浏览器因为firebug的页面元素显示很清晰.用selenium 去定位元素的时候Firefox还有一个非常友好的工具就是firep ...
- [译]Selenium Python文档:目录
作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...
- [译]Selenium Python文档:一、安装
1.1.简介 Selenium Python为使用Selenium WebDriver来编写功能/验证测试提供了一个简单的API接口.通过Selenium Python API,你可以以一种非常直观的 ...
- [译]Selenium Python文档:二、初步开始
2.1.简单使用 如果已经安装好了Selenium Python,你就可以像下面这样编写Python代码来使用它了: from selenium import webdriver from selen ...
随机推荐
- KMP笔记√//找最大子串,前缀自匹配长度
假设s1里找s2,然后s2进去匹配假设在第三位失配那么说明前两位是匹配成功的 如果这时候将s2后移一位相当于将s2的第一位和s2的第二位比较,如果我们已知s1(1)≠s1(2)那么就可以直接后移两位 ...
- Sina App Engine(SAE)入门教程(1)
此教程只针对刚接触SAE的小白用户,资深码农.高手请绕道.首先还是一个经典的实例,hello sae. 创建应用 在注册完账号之后,需要到 http://sae.sina.com.cn/?m=myap ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-007View层
0.login.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- python optparse模块
为了从更快的的使用这个模块,我们会省略一些东西,开始吧. 1. 加载模块 from optparse import OptionParser 2. 实例化OptionParse,可以理解为表明usea ...
- 解决不安装VC运行库(VC2005,VC2008),程序运行出错的方法
因为VS2005以后程序采用了manifest的生成方式,所以发布的时候要和运行库一起发布.但是我们平时开发和发布的时候如果都要客户安装运行库,那就不太方便了.你可以Microsoft下载:http: ...
- C#操作.csv文件Demo
1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...
- 52. N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- 41. First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
This example shows how to use Neural Network Toolbox™ to train a deep neural network to classify ima ...
- Ibatis,Spring整合(注解方式注入)
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...