Python+selenium第一个测试案例
来源:互联网 发布:表格数据合计 编辑:程序博客网 时间:2024/06/14 17:30
1、安装Python35
官网:https://www.python.org/downloads/windows/
我安装的版本是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 webdriverimport timebrowser = 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.pyTraceback (most recent call last): File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start stdout=self.log_file, stderr=self.log_file) File "C:\Users\Python\Python35\lib\subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "C:\Users\Python\Python35\lib\subprocess.py", line 1224, in _execute_child startupinfo)FileNotFoundError: [WinError 2] The system cannot find the file specifiedDuring 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-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__ self.service.start() File "C:\Users\Python\Python35\lib\site-packages\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 <module> browser = webdriver.Firefox() # Get local session of firefox File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__ keep_alive=True) File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__ self.start_session(desired_capabilities, browser_profile) File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute self.error_handler.check_response(response) File "C:\Users\Python\Python35\lib\site-packages\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 matching set of capabilities
按照最后一行的错误信息,从网上找到的原因是,Python3.5.2&selenium3.4.3需要有jdk8&Firefox52以上的环境
我的jdk已经是8版本的,而Firefox还是35版本的,更新Firefox到54版本后,重新执行Python就成功了。
使用chrome
修改test.py的内容:
from selenium import webdriverimport timebrowser = 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.pyTraceback (most recent call last): File "chrome.py", line 4, in <module> browser.get("http://www.baidu.com") File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 268, in get self.execute(Command.GET, {'url': url}) File "C:\Users\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 254, in execute response = self.command_executor.execute(driver_command, params) File "C:\Users\Python\Python35\lib\site-packages\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-packages\selenium\webdriver\remote\remote_connection.py", line 488, in _request resp = self._conn.getresponse() File "C:\Users\Python\Python35\lib\http\client.py", line 1197, in getresponse response.begin() File "C:\Users\Python\Python35\lib\http\client.py", line 297, in begin version, status, reason = self._read_status() File "C:\Users\Python\Python35\lib\http\client.py", line 258, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Users\Python\Python35\lib\socket.py", line 575, in readinto return self._sock.recv_into(b)ConnectionResetError: [WinError 10054] An existing connection was forcibly closed 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第一个测试案例
- 第一个Selenium RC测试案例
- Selenium私房菜系列5-- 第一个Selenium RC测试案例
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
- 使用Python+selenium实现第一个自动化测试脚本
- python-day0003-搭建基于python的selenium自动化测试---第一个简单的测试脚本
- 2、Selenium + Python 实现 UI 自动化测试-第一个自动化测试脚本
- Apache AB 第一个测试案例及测试结果解释
- 自动化测试第一课:selenium + webdriver + python 是什么
- selenium入门第一个脚本(python篇)
- 怎样开始写第一个基于python的selenium脚本
- python+selenium 编写第一个自动化脚本 Ie版
- python+selenium 编写第一个程序 Firefox篇
- Selenium学习笔记之005:第一个selenium+python+webdriver脚本
- 重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源
- 服务器概述、分类及web服务器-Apache服务器的介绍
- springmvc解决返回json的数据格式问题
- 练习1-17 编写一个程序,打印长度大于80个字符的所有输入行。
- KUDU--秒级查询的数据仓库
- Python+selenium第一个测试案例
- 1.Sqoop入门学习
- JNDI学习总结(三)——Tomcat下使用Druid配置JNDI数据源
- ACL 匹配规则 in out 方向
- php 生成csv文件(file_put_contents实现)
- 登陆流程分析
- 小程序微信支付支付流程(类似H5支付)
- 微信分享
- Apriori算法(基于R的应用示例)