使用Python學習selenium測試工具-2:快速入門

           Selenium WebDriver python client可以訪問Selenium WebDriver和Selenium standalone server,開發人員:David        Burns,        Adam Goucher, Maik Röder,Jason        Huggins, Luke        Semerau, Miki Tebeka和Eric Allenin。支持python版本2.6, 2.7, 3.2和3.3。

安裝:

pip install -U selenium

文檔

需要選擇一個合適IDE,要求如下:

  • 代碼完成和智能提示的圖形化代碼編輯器

  • 函數和類的代碼瀏覽器

  • 語法高亮

  • 項目管理

  • 代碼模板

  • 單元測試和調試

  • 源代碼控制支持

推薦:WingIDE,PyCharm,PyDev Eclipse plugin,PyScripter。相關下載地址如下:

實例1:在網站python自動化測試上面尋找webdriver相關的頁面,並輸出相關的網址:

注意:本實例因爲http://automationtesting.vipsinaapp.com及http://automationtesting.sinaapp.com已經因爲新浪收費原因關閉匿名用戶查詢,已經不能執行。

#!/usr/bin/env python
# encoding: utf-8
import time

from selenium import webdriver

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://automationtesting.sinaapp.com/")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("webdriver")
search_field.submit()



time.sleep(6)
products = driver.find_elements_by_xpath("//a[@class='searchable']")
# get the number of anchor elements found
print "Found " + str(len(products)) + " pages:"

# iterate through each anchor element and
# print the text that is name of the product
for product in products:
    print product.get_attribute('href')

# close the browser window
driver.quit()

selenium.webdriver實現了瀏覽器驅動類,涉及 Firefox, Chrome, Internet Explorer,        Safari等瀏覽器及用於測試遠程機器的類RemoteWebDriver。

driver.implicitly_wait(30)表示最多等待頁面打開的時間爲30秒。

執行結果:

Found 2 pages:
http://automationtesting.sinaapp.com/blog/python_selenium1
http://automationtesting.sinaapp.com/blog/appium

實例2打開網址,搜索產品,並列出產品名。實例的網站爲:magentocommerce,代碼下載地址:learnsewithpython

注意:上面演示的網址可能需要翻牆

from selenium import webdriver

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()

# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")

# get the number of anchor elements found
print "Found " + str(len(products)) + " products:"

# iterate through each anchor element and
# print the text that is name of the product
for product in products:
    print product.text

# close the browser window
driver.quit()

執行結果:

Found 2 products:
MADISON EARBUDS
MADISON OVEREAR HEADPHONES

跨瀏覽器支持

IE

selenium下載頁面地址。爲了在Internet Explorer上面執行,需要下載配置InternetExplorerDriver服務,它是測試腳本和Internet Explorer之間的膠水。

在上述下載頁面搜索InternetExplorerDriver,下載32位或者64位版本,並解壓到合適的目錄。在IE7以上的版本,選擇Tools 菜單下面的Internet Options,在彈出窗口選擇Security標籤,把每個zone的Protected Mode設置爲關或者開啓的情況都設置爲中。另外要特別注意IE的縮放要選擇爲100%才正確地進行座標對應。

實例1:

#!/usr/bin/env python
# encoding: utf-8
import time

from selenium import webdriver

ie_driver_path = "e:\IEDriverServer.exe"

# create a new Firefox session
driver = webdriver.Ie(ie_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://automationtesting.sinaapp.com/")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("webdriver")
search_field.submit()



time.sleep(6)
products = driver.find_elements_by_xpath("//a[@class='searchable']")
# get the number of anchor elements found
print "Found " + str(len(products)) + " pages:"

# iterate through each anchor element and
# print the text that is name of the product
for product in products:
    print product.get_attribute('href')

# close the browser window
driver.quit()
實例2:

import os
from selenium import webdriver

# get the path of IEDriverServer
# dir = os.getcwd()
ie_driver_path = "e:\IEDriverServer.exe"

# create a new Internet Explorer session

driver = webdriver.Ie(ie_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()

# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")

# get the number of anchor elements found
print "Found " + str(len(products)) + " products:"

# iterate through each anchor element and
# print the text that is name of the product
for product in products:
    print product.text

# close the browser window
driver.quit()

執行結果和火狐的類似。

參考資料:

Chrome

ChromeDriver由Chromium開發,安裝方法和IE的類似,但是不需要設置Protected Mode。 實例1:

#!/usr/bin/env python
# encoding: utf-8
import time

from selenium import webdriver

# get the path of chromedriver
chrome_driver_path = r"e:\chromedriver.exe"
#remove the .exe extension on linux or mac platform

# create a new Chrome session
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://automationtesting.sinaapp.com/")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("webdriver")
search_field.submit()



time.sleep(6)
products = driver.find_elements_by_xpath("//a[@class='searchable']")
# get the number of anchor elements found
print "Found " + str(len(products)) + " pages:"

# iterate through each anchor element and
# print the text that is name of the product
for product in products:
    print product.get_attribute('href')

# close the browser window
driver.quit()
實例2:

import os
from selenium import webdriver

# get the path of chromedriver
chrome_driver_path = r"e:\chromedriver.exe"
#remove the .exe extension on linux or mac platform

# create a new Chrome session
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()

# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")

# get the number of anchor elements found
print "Found " + str(len(products)) + " products:"

# iterate through each anchor element and
# print the text that is name of the product
for product in products:
    print product.text

# close the browser window
driver.quit()
參考資料:
發佈了28 篇原創文章 · 獲贊 21 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章