1. 首先配置eclipse+pydev環境
a) 安裝jdk
b) 安裝eclipse
c)安裝Python2.7,安裝Pydev
2. 去官網(http://seleniumhq.org/download/)下載Selenium Python檔案
點擊python的Download ,進入selenium python的下載頁面,https://pypi.python.org/pypi/selenium
3.下載Selenium-2.35.0.tar.gz,然後解壓進行安裝, 執行命令python setup.py install
或者在目錄C:\Python27\Scripts下執行命令:pip install -U selenium安裝selenium,不過需要先安裝setuptools-py27(https://pypi.python.org/pypi/setuptools/1.0)
4. ChromeDriverhttps://code.google.com/p/chromedriver/downloads/detail?name=chromedriver_win32_2.2.zip&can=2&q=,然後把chromedriver解壓到C:\Python27目錄下(chromedriver需要用最新的版本)
5 .執行測試指令碼:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome() # Get local session of firefox
browser.get("http://www.baidu.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.close()
7.開啟IE失敗方法的解決
問題如下:(我使用的是Python)
WebDriverException: Message: u'Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones.'
後來查了好些資料,找到了兩種解決辦法:
1)修改IE的安全性原則,就像Exception裡面提示的那樣。
2)在產生webdriver對象之前先執行這些代碼:
1 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2 DesiredCapabilities.INTERNETEXPLORER['ignoreProtectedModeSettings'] = True