zoukankan      html  css  js  c++  java
  • 交互

    获取文本值

    人们通常希望检索元素中包含的innerText值。这会返回单个字符串值。请注意,这只会返回页面上显示的可见文本。

    element = driver.find_element_by_id("element_id")
    element.text

    用户输入 - 填写表单

    我们已经看到如何将文本输入到textarea或文本字段中,但其他元素呢?可以“切换”复选框的状态,并且可以使用“单击”来设置类似于所选OPTION标记的内容。处理SELECT标签并不算太糟糕:

    select = driver.find_element_by_tag_name("select")
    allOptions = select.find_elements_by_tag_name("option")
    for option in allOptions:
        print "Value is: " + option.get_attribute("value")
        option.click()

    这将在页面上找到第一个“SELECT”元素,并依次循环选择每个选项,打印出它们的值并依次选择每个选项。正如你会注意到的,这不是处理SELECT元素的最有效方式。WebDriver的支持类包括一个名为“Select”的类,它提供了与这些类交互的有用方法。

    from selenium.webdriver.support.ui import Select
    select = Select(driver.find_element_by_tag_name("select"))
    select.deselect_all()
    select.select_by_visible_text("Edam")

    这将从页面上的第一个SELECT中取消选择所有OPTION,然后选择显示文本“Edam”的OPTION。

    完成表格填写后,您可能需要提交。一种方法是找到“提交”按钮并点击它:

    driver.find_element_by_id("submit").click()

    或者,WebDriver在每个元素上都有“提交”的便利方法。如果你在一个表单中的元素上调用它,WebDriver会遍历DOM直到找到包含表单,然后调用它。如果元素不在表单中,NoSuchElementException则会抛出:

    element.submit()

    在Windows和框架之间移动

    一些Web应用程序有许多框架或多个窗口。WebDriver支持使用“switchTo”方法在命名窗口之间移动:

    driver.switch_to.window("windowName")

    driver现在所有的呼叫将被解释为指向特定的窗口。但你怎么知道这个窗口的名字?看看打开它的javascript或链接:

    <a href="somewhere.html" target="windowName">Click here to open a new window</a>

     或者,可以将“窗口句柄”传递给“switchTo().window()” 方法。知道这一点,可以遍历每个打开的窗口,如下所示:

    for handle in driver.window_handles:
        driver.switch_to.window(handle)

    您也可以在frame之间切换(或者切换到iframe):

    driver.switch_to.frame("frameName")

    弹出对话框

    触发了一个打开弹出窗口的操作后,可以使用以下命令访问该alert:

    alert = driver.switch_to.alert
    # usage: alert.dismiss(), etc.

    这将返回当前打开的alert对象。通过这个对象,现在可以accept, dismiss, 阅读其内容,甚至可以输入提示。此界面在警报,确认和提示方面同样适用。

    导航:历史和位置

    早些时候,我们介绍了使用“get”命令:driver.get("http://www.example.com")导航到页面正如所看到的,WebDriver有许多更小的,以任务为中心的界面,导航是一项有用的任务。因为加载页面是一个基本的要求,所以执行此操作的方法将存在于WebDriver主界面上,但它只是以下内容的同义词:

    driver.get("http://www.example.com")  # python doesn't have driver.navigate

    重申:“navigate().to()” and “get()”完全一样。

    “导航”界面还提供了在浏览器历史记录中前后移动的功能:

    driver.forward()
    driver.back()

    请注意,此功能完全取决于底层浏览器。

    Cookies

    首先,需要位于Cookie有效的域上。如果在开始与网站互动之前尝试预设Cookie,并且主页很大/需要一段时间才能加载替代方法,则可以在网站上找到较小的页面(通常404页面很小,例如http://示例.com / some404page)。

    # Go to the correct domain
    driver.get("http://www.example.com")
    
    # Now set the cookie. Here's one for the entire domain
    # the cookie name here is 'key' and its value is 'value'
    driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})
    # additional keys that can be passed in are:
    # 'domain' -> String,
    # 'secure' -> Boolean,
    # 'expiry' -> Milliseconds since the Epoch it should expire.
    
    # And now output all the available cookies for the current URL
    for cookie in driver.get_cookies():
        print "%s -> %s" % (cookie['name'], cookie['value'])
    
    # You can delete cookies in 2 ways
    # By name
    driver.delete_cookie("CookieName")
    # Or all of them
    driver.delete_all_cookies()

    更改用户代理

    使用Firefox驱动程序很容易

    profile = webdriver.FirefoxProfile()
    profile.set_preference("general.useragent.override", "some UA string")
    driver = webdriver.Firefox(profile)

    拖放

    以下是使用Actions类执行拖放操作的示例。本机事件需要启用。

    from selenium.webdriver.common.action_chains import ActionChains
    element = driver.find_element_by_name("source")
    target =  driver.find_element_by_name("target")
    
    ActionChains(driver).drag_and_drop(element, target).perform()
  • 相关阅读:
    使用 libevent 和 libev 提高网络应用性能
    An existing connection was forcibly closed by the remote host
    各种浏览器的兼容css
    vs输出窗口,显示build的时间
    sass
    网站设置404错误页
    List of content management systems
    css footer not displaying at the bottom of the page
    强制刷新css
    sp_executesql invalid object name
  • 原文地址:https://www.cnblogs.com/weiweim/p/8461348.html
Copyright © 2011-2022 走看看