2013-05-29 82 views
1

鉻我有以下代碼測試使用硒和Python

import time 

from selenium import webdriver 
import selenium.webdriver.chrome.service as service 

chromedriver_path = "/Users/stephen/Downloads/chromedriver2_mac32_0.8/chromedriver" 

chromium_path = "/Users/stephen/Downloads/chrome-mac/Chromium.app/Contents/MacOs/Chromium" 

service = service.Service(chromedriver_path) 
service.start() 
capabilities = {'chrome.binary': chromium_path} 
driver = webdriver.Remote(
    service.service_url, 
    desired_capabilities=capabilities) 
driver.get('http://www.google.com/xhtml'); 
time.sleep(5) # Let the user actually see something! 
driver.quit() 

不幸的是,當我運行上面的Python腳本,硒很客氣地完全​​忽略了,我想用Chromium的事實,而是使用我的默認Google Chrome。清楚的是,它確實是腳本指定的內容,它只是使用Chrome而不是Chromium。

顯然,我做錯了什麼。我將我的嘗試建立在以下幾頁之外。

https://code.google.com/p/chromedriver/wiki/GettingStarted

http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html?highlight=capabilities

什麼我需要做的,用硒(在Python)的網絡瀏覽器?

回答

6

desired_capabilities選項用於傳遞給通用硒驅動程序代碼的選項。鉻驅動程序使用的選項(包括chrom(e | ium)二進制位置)使用chrome_options按如下方式傳入:

from selenium.webdriver.chrome.options import Options 
opts = Options() 
opts.binary_location = chromium_path 
driver = webdriver.Chrome(chrome_options=opts)