Python+selenium第一个测试案例

1、安装Python35

官网:https://www.python.org/downloads/windows/

我安装的版本是v3.5.2,Windows系统

安装过程中记得勾选安装到环境的复选框:Add Python 3.5 to PATH,不然安装完成后还需要手动进行环境变量的配置。

Python+selenium第一个测试案例_第1张图片

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 
    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,从网上查找的解决方法看,需要手动下载该文件

官网:https://github.com/mozilla/geckodriver/releases
并将解压出来的geckodriver.exe放到Python的目录下,C:\Users\Python\Python35\

重新执行Python:python test.py

这时仍旧报错,但是错误信息换了一个:

Traceback (most recent call last):
  File "test.py", line 8, in 
    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:http://npm.taobao.org/mirrors/chromedriver

将chromedriver.exe配置到环境变量中,我直接放到了C:\Users\Python\Python35\,就不用再配置环境了


执行报错:

E:\python\selenium>python chrome.py
Traceback (most recent call last):
  File "chrome.py", line 4, in 
    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版本数据:

http://chromedriver.storage.googleapis.com/2.29/notes.txt

我下载的是2.9版本,chrome只能选择31-34,因为我的chrome是57版本的,应该下载2.29(还以为2.9是最新的……)

各版本chrome下载:

http://google-chrome.en.uptodown.com/mac/old(好像没有用了)

重新执行成功

你可能感兴趣的:(Python,selenium)