<译>Selenium Python Bindings 3 - Navigating

时间:2014-05-15 09:50:11   收藏:0   阅读:393

  当你想要通过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将有效:
    bubuko.com,布布扣
    # 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)
    bubuko.com,布布扣

     








<译>Selenium Python Bindings 3 - Navigating,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!