1 python下載安裝python
python早已安裝,再也不多說。由於開發使用的python2.7,因此一樣使用2.7git
2 打開Powershell, 輸入python -m pip install selenium 。github
提示: web
Requirement already satisfied (use --upgrade to upgrade): selenium in e:\python27\lib\site-packages
You are using pip version 7.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.shell
3 再輸入 python -m pip install --upgrade pip 。瀏覽器
4 使用python IDLE, 輸入例子代碼session
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time browser = webdriver.Firefox() browser.get('http://www.yahoo.com') assert 'Yahoo' in browser.title elem = browser.find_element_by_name('p') elem.send_keys('seleniumhq'+ Keys.RETURN) time.sleep(0.2) try:
browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]") except NoSuchElementException: assert 0, "can't find seleniumhq" browser.quit()
報錯: python2.7
Traceback (most recent call last):
File "C:/Users/william/Desktop/selenium/seleniumhq.py", line 6, in <module>
browser = webdriver.Firefox()
File "E:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__
self.service.start()
File "E:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'geckodriver' executable needs to be in PATH. ide
5 打開google, 搜索 WebDriverException: Message: 'geckodriver' executable needs to be in PATH。 或者百度,搜索 : selenium geckodriver。ui
6 緣由是, firefox 47以上版本,須要下載第三方driver,即geckodriver
7 打開 http://docs.seleniumhq.org/download/ 。 下載 Mozilla GeckoDriver ,解壓。這裏,我將exe文件放到python的Scripts中,免了新加path。
8 執行python,發現仍報錯。
Traceback (most recent call last):
File "C:/Users/william/Desktop/selenium/seleniumhq.py", line 6, in <module>
browser = webdriver.Firefox()
File "E:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 150, in __init__
keep_alive=True)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
看意思,應該是firefox的安裝路徑沒有找到的緣由。google一下。
9 python代碼中添加
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)
再執行,沒有報錯了。執行完後,python shell會顯示斷言的提示:can't find seleniumhq, 而且瀏覽器沒有退出。這是爲何呢? 結果應該是find seleniumhq啊
難道是 assert 0, "can't find seleniumhq" 後面的0的緣故?
10 百度搜索 assert 0。 後面加0,表明永遠爲假,即永遠會顯示 can't find seleniumhq。但這個的發生是在exception以後啊。
11 去掉0試一試。再也不顯示斷言提示
12 若是將xpath中的條件改一改呢? 改爲 http://seleniumhqwww.org 。 仍然沒有顯示斷言,並瀏覽器退出。這裏就不對了。由於這個xpath應該是匹配不到的。
13 再查斷言assert的用法
assert的用法,常見的文章中提到的都是,assert用來聲明某個條件是真的。若是非真,就引起一個錯誤,拋出這個錯誤AssertError幷包含錯誤信息。
例如 assert x>0,"x is not zero or negative"
這樣的話,從語法上講,assert 0, "can't find seleniumhq" 沒有問題。那麼就是真沒有find element
14 是否是sleep(0.2)時間過短,element尚未出現形成的呢? 改0.2爲5,再分別試,OK。解決。