Web自动化测试(3): Selenium Web Driver 如何操作web页面。

来源:互联网 发布:oracle sql nvl函数 编辑:程序博客网 时间:2024/06/09 16:56

背景知识:

            建议想快速理解本页内容的同学们学习下Jquery,有利于你更好的了解 Selenium Web Driver的函数。因为它们有些相似。JQuery的文档很多,如果想快速上手Jquery,可以访问:http://www.w3school.com.cn/jquery/。当然你也可以直接学习Selenium Web Driver的函数:http://docs.seleniumhq.org/docs/03_webdriver.jsp。

           使用firefox firebug插件方便我们更快查看页面源码。

正文:

           Selenium-WebDriver API Commands and Operations:

           1. 访问一个web页面。

  from selenium import webdriver    driver = webdriver.Firefox()    driver.get('http://seleniumhq.org/')

           2.定位页面的控件。请使用Firefox浏览器,并安装Firebug,使用其它浏览器请安装develop tool。方便我们看见页面源码。对于页面源码,我们要熟悉常用的页面元素,比如div,iframe,span,p ,明白它在页面表示什么意思,这里需要涉及html相关知识。

                2.1 By ID(find_element_by_id) 

<div id="coolestWidgetEvah">...</div>
element = driver.find_element_by_id("coolestWidgetEvah")orfrom selenium.webdriver.common.by import Byelement = driver.find_element(by=By.ID, value="coolestWidgetEvah")  

                2.2 By Class Name(find_elements_by_class_name)--注意:因为页面为同一个class的元素可以很多,而一个id只能表示一个元素,所以 2.1 是find_element 而2.2是find_elements.

<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
</pre><pre code_snippet_id="120077" snippet_file_name="blog_20131220_4_8468142" class="python" name="code">cheeses = driver.find_elements_by_class_name("cheese")orfrom selenium.webdriver.common.by import Bycheeses = driver.find_elements(By.CLASS_NAME, "cheese")

                2.3 By Tag Name(find_element_by_tag_name)

<iframe src="..."></iframe>
frame = driver.find_element_by_tag_name("iframe")orfrom selenium.webdriver.common.by import Byframe = driver.find_element(By.TAG_NAME, "iframe")

                2.4  By Name(find_element_by_name)

<input name="cheese" type="text"/>
cheese = driver.find_element_by_name("cheese")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.NAME, "cheese")

                2.5 By Link Text

<a href="http://www.google.com/search?q=cheese">cheese</a>>
cheese = driver.find_element_by_link_text("cheese")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.LINK_TEXT, "cheese")

                2.6 By Partial Link Text(find_element_by_partial_link_text)

<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
cheese = driver.find_element_by_partial_link_text("cheese")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")

                2.7 By CSS(find_element_by_css_selector)

<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")

           2.8 By XPATH(XPATH)(下一节我会对XPATH做详细介绍)

<input type="text" name="example" /><INPUT type="text" name="other" />
inputs = driver.find_elements_by_xpath("//input")orfrom selenium.webdriver.common.by import Byinputs = driver.find_elements(By.XPATH, "//input")

               2.9  Using javascript(下面是一个jQuery的例子)    

element = driver.execute_script("return $('.cheese')[0]")


0 0