当你想要通过webdriver导航到一个链接,正常的方式点是通过调用get方法:

driver.get("http://www.google.com")
  1. 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()
  2. 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()
  3. 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)
  4. 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()
  5. Popup dialogs

    Selenium webdriver 内置了用于处理弹出对话框的支持。当你点击,将打开一个弹出窗口中,您可以访问警报,执行以下操作:

    alert = driver.switch_to_alert()

    这将返回当前打开的警示对象。有了这个对象,你现在可以接受,拒绝,阅读其内容,甚至键入一个提示。这个接口同样适用于警示,确认,提示。

  6. Navigation : history and location
    driver.forward()
    driver.back()
  7. 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的更多相关文章

  1. <译>Selenium Python Bindings 1 - Installation

    Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...

  2. <译>Selenium Python Bindings 5 - Waits

    如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...

  3. <译>Selenium Python Bindings 2 - Getting Started

    Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...

  4. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  5. <译>Selenium Python Bindings 4 - Locating Eelements

    有各种不同的策略来定位页面中的元素.你可以使用最合适定位方式用于你的用例.Selenium提供了以下方法来定位页面中的元素: find_element_by_id find_element_by_na ...

  6. selenium python bindings 元素定位

    1. 辅助 Firepath Firefox是所有做前端的必不可少的浏览器因为firebug的页面元素显示很清晰.用selenium 去定位元素的时候Firefox还有一个非常友好的工具就是firep ...

  7. [译]Selenium Python文档:目录

    作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...

  8. [译]Selenium Python文档:一、安装

    1.1.简介 Selenium Python为使用Selenium WebDriver来编写功能/验证测试提供了一个简单的API接口.通过Selenium Python API,你可以以一种非常直观的 ...

  9. [译]Selenium Python文档:二、初步开始

    2.1.简单使用 如果已经安装好了Selenium Python,你就可以像下面这样编写Python代码来使用它了: from selenium import webdriver from selen ...

随机推荐

  1. Oracle的学习三:java连接Oracle、事务、内置函数、日期函数、转换函数、系统函数

    1.java程序操作Oracle java连接Oracle JDBC_ODBC桥连接 1.加载驱动: Class.forName("sun.jdbc.odbc.JdbcodbcDriver& ...

  2. 套题T2

    数学(math.cpp) DXY的数学很差... 对于所有1<=i<=N求(2^i – i^2)能被7整除的个数.(N<=1000000) 样例输入: 3 样例输出: 1 你在代码中 ...

  3. Use windows batch script to create menu

    Background Recently, I find that we need  to  type some very long gradle commands to run build, chec ...

  4. Python Snippet

    python按行读取文件,如何去掉换行符"\n" for line in file.readlines(): line=line.strip('\n') python没有subst ...

  5. 几种USB控制器类型:OHCI,UHCI,EHCI,xHCI

    http://smilejay.com/2012/10/usb_controller_xhci/ 遇到过一些关于USB的东西(如下),一直没搞明白什么USB1.0/1.1/2.0/3.0之类的,当然我 ...

  6. 【多媒体封装格式详解】---MKV

    http://blog.csdn.net/tx3344/article/details/8162656# http://blog.csdn.net/tx3344/article/details/817 ...

  7. parent children

    class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...

  8. 在PowerDesigner中设计物理模型1——表和主外键

    原文:在PowerDesigner中设计物理模型1--表和主外键 在PD中建立物理模型由以下几种办法: 直接新建物理模型. 设计好概念模型,然后由概念模型生成物理模型. 设计好逻辑模型,然后由逻辑模型 ...

  9. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  10. 设计模式之Inheritance versus Parameterized Types 继承和参数化类型

    Another (not strictly object-oriented)technique for reusing functionality is through parameterized t ...