1. 程式人生 > >Python+selenium第一個測試案例

Python+selenium第一個測試案例

1、安裝Python35

我安裝的版本是v3.5.2,Windows系統

安裝過程中記得勾選安裝到環境的複選框:Add Python 3.5 to PATH,不然安裝完成後還需要手動進行環境變數的配置。


2、下載selenium外掛

在cmd中輸入命令:python-m pip install selenium

*如果提示Python不是內部或外部命令,就需要將Python路徑配置到環境變數去

3、新建一個txt檔案,取名並修改後綴為test.py,將以下程式碼複製並儲存:

from selenium import webdriver
import time
browser = webdriver.Firefox() 
browser.get("http://www.baidu.com") 
time.sleep(5)
browser.close()
4、執行Python檔案

可直接雙擊test.py執行,更靠譜的方式還是在cmd中輸入命令:python test.py

使用Firefox

如果這是第一次使用selenium,可能會報以下這個錯誤:

E:\python\selenium>python test.py
Traceback (most recent call last):
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\common\service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:\Users\Python\Python35\lib\subproce
ss.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Python\Python35\lib\subproce
ss.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    browser = webdriver.Firefox() # Get local session of firefox
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
    self.service.start()
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable
 needs to be in PATH.

最後一行錯誤資訊提示缺少geckodriver,從網上查詢的解決方法看,需要手動下載該檔案

重新執行Python:python test.py

這時仍舊報錯,但是錯誤資訊換了一個:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    browser = webdriver.Firefox() # Get local session of firefox
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
    keep_alive=True)
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to find a matchin
g set of capabilities

按照最後一行的錯誤資訊,從網上找到的原因是,Python3.5.2&selenium3.4.3需要有jdk8&Firefox52以上的環境

我的jdk已經是8版本的,而Firefox還是35版本的,更新Firefox到54版本後,重新執行Python就成功了。

使用chrome

修改test.py的內容:

from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get("http://www.baidu.com")
time.sleep(5)
browser.close()

將chromedriver.exe配置到環境變數中,我直接放到了C:\Users\Python\Python35\,就不用再配置環境了

執行報錯:

E:\python\selenium>python chrome.py
Traceback (most recent call last):
  File "chrome.py", line 4, in <module>
    browser.get("http://www.baidu.com")
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\webdriver.py", line 268, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\webdriver.py", line 254, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\remote_connection.py", line 464, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\Python\Python35\lib\site-pac
kages\selenium\webdriver\remote\remote_connection.py", line 488, in _request
    resp = self._conn.getresponse()
  File "C:\Users\Python\Python35\lib\http\cli
ent.py", line 1197, in getresponse
    response.begin()
  File "C:\Users\Python\Python35\lib\http\cli
ent.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Python\Python35\lib\http\cli
ent.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\Python\Python35\lib\socket.p
y", line 575, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly close
d by the remote host

按照網上的說法,應該要升級chrome或者降級chrome,由於許可權限制,沒有進行進一步的測試。

在chromedriver下載的網站中找到note.txt檔案,上面有記載各版本chromedriver所支援的chrome版本資料:

我下載的是2.9版本,chrome只能選擇31-34,因為我的chrome是57版本的,應該下載2.29(還以為2.9是最新的……)

各版本chrome下載:

重新執行成功