您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
如何在Python中使用Selenium?
作者:虞大膽 發(fā)布時(shí)間:[ 2016/9/30 14:12:31 ] 推薦標(biāo)簽:功能測(cè)試 Selenium 軟件測(cè)試工具

  近基于 selenium 寫了一個(gè) python 小工具,記錄下學(xué)習(xí)記錄,自己運(yùn)行的環(huán)境是 Ubuntu 14.04.4 , Python 2.7 , Chromium 49.0 , ChromeDriver 2.16
  selenium簡(jiǎn)介
  selenium提供了一個(gè)通用的接口,可模擬用戶來(lái)操作瀏覽器,比如用于自動(dòng)化測(cè)試等.
  selenium的核心是 WebDriver ,它提供了一組接口,這些接口能夠操作各種跨平臺(tái)的瀏覽器.
  各大瀏覽器廠商.
  各大瀏覽器廠商也支持Selenium,將其作為瀏覽器的一部分.
  selenium 工具集提供了 WebDriver , Selenium IDE , Selenium-Grid 等
  Selenium 1.0 + WebDriver = Selenium 2.0
  Selenium WebDriver 是 Selenium Remote Control(Selenium-RC) 的繼承者.
  · WebDriver 提供了更簡(jiǎn)單和簡(jiǎn)潔的接口,克服了 Selenium-RC API 一些限制.
  · 相比 Selenium 1.0 , WebDriver 是面向?qū)ο笫降姆⻊?wù).
  · WebDriver 驅(qū)動(dòng)瀏覽器更有效率,提供了比 Selenium 1.0 更多的功能
  · Selenium RC 只能在單機(jī)上運(yùn)行, WebDriver 則提供了遠(yuǎn)程操作的功能
  selenium基本使用
  selenium運(yùn)行需要什么
  主要包括三部分: selenium selenium , 瀏覽器driver , 瀏覽器
  selenium selenium 是一組通用的接口,而不同的瀏覽器提供其自身的 driver (大部分是官方的),瀏覽器則被模擬控制操作的終端.
  安裝
pip install selenium --upgrade
apt-get install chromium-browser
wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux`getconf LONG_BIT`.zip
unzip chromedriver_linux32.zip
cp chromedriver /usr/local/share
chmod +x /usr/local/share/chromedriver
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
ln -s /usr/bin/chromedriver /usr/local/share/chromedriver
  簡(jiǎn)單的使用
from selenium import webdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get('http://mail.sina.net');
print(driver.title)
  API使用
  可參考 /usr/local/lib/python2.7/dist-packages/selenium
  Chrome WebDriver
  selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None)
  ChromeOptions
  可以通過(guò) ChromeDriver session 配置 ChromeDriver session
  ChromeDriver convenient methods for setting ChromeDriver-specific capabilities
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-logging')
chrome_options.add_experimental_option('prefs', {'download.default_directory':
'/tmp'})
chrome_options.binary_location='/usr/bin/chromium-browser'
driver = webdriver.Chrome(chrome_options=chrome_options)
  直接使用DesiredCapabilities
  ChromeOptions是構(gòu)建在DesiredCapabilities之上的,為了使用DesiredCapabilities,必須
  知道capability的Key/value對(duì).
  chrome_options = Options()
  capabilities={}
  capabilities['platform'] = "WINDOWS"
  capabilities['version'] = "10"
  capabilities.update(chrome_options.to_capabilities())
  driver = webdriver.Chrome(desired_capabilities=capabilities)
  chromedriver運(yùn)行方式
  The ChromeDriver class 不斷的創(chuàng)建實(shí)例,會(huì)浪費(fèi)很多的時(shí)間,可以通過(guò)兩個(gè)方式解決.
  使用ChromeDriverService
  import selenium.webdriver.chrome.service as service
  service = service.Service('/usr/bin/chromedrive')
  service.start()
  capabilities = { }
  driver = webdriver.Remote(service.service_url, capabilities)
  driver.get('http://mail.sina.net');
  print(driver.title)
  開(kāi)啟單獨(dú)的ChromeDriver服務(wù)
  ./chromedriver
  driver = webdriver.Remote('http://127.0.0.1:9515', DesiredCapabilities.CHROME)
  driver.get('http://mail.sina.net');
  RemoteWebDriverServer
  The RemoteWebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a Java servlet, which can be hosted in any modern JEE app server. The server will always run on the machine with the browser you want to test.
  wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
  java -jar selenium-server-standalone-2.53.0.jar
  from selenium import webdriver
  from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub',des
  desired_capabilities=DesiredCapabilities.CHROME)
  driver.get('http://mail.sina.net');
  其他
  參考網(wǎng)站:
  官方網(wǎng)站 , 官方github
  ChromeDriver
  Python api
  Selenium非官方文檔
  used pyvirtualdisplay to simulate a display:
  由于使用的是Ubuntu服務(wù)器版,所以無(wú)法運(yùn)行chrome,通過(guò)pyvirtualdisplay解決該問(wèn)題
sudo apt-get install Xvfb
sudo pip install pyvirtualdisplay
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get('http://mail.sina.net');
print(driver.title)
display.stop()
  Explicit Wait:
  由于是模擬用戶操作瀏覽器,瀏覽器加載需要時(shí)間,為確保成功加載完成才繼續(xù)執(zhí)行下去,解決方案如下
  from selenium.webdriver.support.ui import WebDriverWait
  driver = webdriver.Chrome()
  driver.get('http://mail.sina.net/login');
  el =  WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("loginfromemail"))
  print(el.text)

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd