Python3+Selenium环境配置
一、所需工具
1、Python3.6安装包
2、Selenium安装包(selenium-server-standalone-3.8),如果是Python3的话可以不用下载selenium压缩包,Python3有自带的,直接输入命令安装即可
3、Chromedriver驱动(Chromedriver2.3)
4、Firefox驱动(geckodriver0.18)
5、Chrome浏览器(chrome v.59)
6、Firefox浏览器(Firefox v.54)
7、Pycharm
二、工具安装
1、安装Python,安装完设置环境变量(将Python安装路径放入path下)
2、selenium安装:cmd首先进入E:\Python\Scripts目录下,然后输入pip install -U selenium,等待安装完成即可
3、pip安装:将pip工具包解压到指定盘,cmd命令进入该盘下,输入命令python setup.py install;再切换到E:\Python\Scripts 目录下输入E:\Python\Scripts > easy_install pip
4、setuptools安装:将setuptools工具包解压到指定盘里,进入cmd,执行python ez_install.py即可
5、将下载好的Chromedriver和geckodriver驱动放到Python安装路径下
注意:Chromedriver和geckodriver驱动一定要和电脑上的浏览器版本对应,否则代码运行会报错
Chromedriver和chrome浏览器对应版本

代码:
from selenium import webdriver
import time
# browser=webdriver.Chrome()
browser=webdriver.Firefox()
url='http://www.baidu.com'
#通过get方法获取当前url打印
print("now access %s" %(url))
browser.get(url)
time.sleep(2)
browser.find_element_by_xpath("//input[@id='kw']").send_keys('Selenium')
browser.find_element_by_id('su').click()
# browser.find_element_by_css_selector("input[value$='下']")
time.sleep(3)
print("成功搜索")
browser.quit()
以上代码如果能正常运行说明环境配置成功
PS:以下三个问题是安装过程中常遇到的坑
遇到第一个坑:'geckodriver' executable needs to be in PATH
1.如果启动浏览器过程中报如下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__
self.service.start()
File "D:\test\python3\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.
2.这个是因为最新的selenium3.0启动firefox需要geckodriver.exe这个驱动文件。
3.下载之后,配置到环境变量path下(可以直接放python根目录)
遇到第二坑:Expected browser binary location, but unable to find binary in default location
1.如果启动浏览器过程中报如下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
keep_alive=True)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.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
2.这个是因为firefox.exe这个文件也需要配置到环境变量path下
3.这个路径就是安装完firefox后,找到firefox.exe这个文件的地址,加到path下

遇到第三坑:Unsupported Marionette protocol version 2, required 3
1.如果启动浏览器过程中出现如下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
keep_alive=True)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3
2.这个错误原因是firefox版本过低了,最新的selenium3.0版本支持firefox47以上的版本,升级版本就可以了
Python3+Selenium环境配置的更多相关文章
- Python - Win10系统下Python3.x环境配置
Win10系统下Python3.x环境配置 https://blog.csdn.net/qq_41952474/article/details/82630551
- C# selenium环境配置
1.下载C#selenium selenium官网: http://www.seleniumhq.org/download/ 下载后解压: 打开net35后,将里面的dll文件添 ...
- python+selenium环境配置及浏览器调用
最近在学习python自动化,从项目角度和技术基础角度出发,我选择了python+selenium+appium的模式开始我的自动化测试之旅: 一.python安装 二.python IDE使用简介 ...
- selenium环境配置和八大元素定位
一.环境配置 1.selenium下载安装 安装一:pip install selenium(多数会超时安装失败) 安装二:pip install -i https://pypi.tuna.tsing ...
- Python3爬虫环境配置
一.包管理工具Anaconda安装 1.安装直接在官网下载最新版本的 https://www.continuum.io/downloads 2.配置环境变量PATH:C:\Users\Administ ...
- Python+Selenium 环境配置之Firefox,IE,Chrome几种浏览器运行
Selenium(Webdriver)支持Firefox,IE,Chrome等多个浏览器.很多人可能装环境时遇到很多问题,下面简单聊聊如何配置测试这几个浏览器以及相关通过简单的实例来测试. 1.Fir ...
- 【Selenium】idea的selenium环境配置
1.maven配置 下载地址:http://maven.apache.org/download.cgi# 下载内容:apache-maven-3.5.0-bin.zip 环境变量:M2_HOME:E: ...
- Java - Selenium 环境配置
1. 安装Java JDK - 文件自己下 2. 配置环境变量-重要! 我的电脑-属性-高级-环境变量 添加 CLASSPATH 值 .;%JAVA_HOME%\lib;%JAVA_HOME%\li ...
- python+selenium 环境配置
配置环境: python:3.5 selenium:3.3.0 安装方式:python pip install -u selenium windows: 10 firefox:52 因为firefox ...
随机推荐
- Android Studio 运行出现 Multiple dex files define Landroid/support/annotation/AnimRes 解决方法
引入的工程的android-support-v4.jar版本跟自己工程的android-support-v4.jar的版本不一样
- Oracle的NVL函数用法
从两个表达式返回一个非 null 值. 语法 NVL(eExpression1, eExpression2) 参数eExpression1, eExpression2 如果 eExpression1 ...
- VS2013 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService
全是2012版本的 没找到2013的! 控制面板>程序>程序和功能 找到如下选中软件右击修复 即可 需关闭VS2013 参考:https://blog.csdn.net/zhaoyun9 ...
- Java知多少(53)使用Java创建自己的异常子类
尽管Java的内置异常处理大多数常见错误,你也许希望建立你自己的异常类型来处理你所应用的特殊情况.这是非常简单的:只要定义Exception的一个子类就可以了(Exception当然是Throwabl ...
- [UI] 04 - Bootstrap: layout & navigation
前言 一.资源 From: http://www.imooc.com/code/2881 Ref: http://www.runoob.com/try/bootstrap/layoutit/[拖拽布局 ...
- TP5分页类
<?php class Page { public $page; //当前页 public $total; //总记录数 public $listRows; //每页显示记录数 private ...
- windows cmd命令显示UTF8设置
windows cmd命令显示UTF8设置 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况 ...
- Inside The C++ Object Model(一)
============================================================================1-1. 关键字struct和class的一点区 ...
- 我的第一篇paper
找实习虽然只为自己争取到一个秋季的绿色通道,但可喜的是,我投的几篇paper,终于中了一篇. 现在可以在英文数据库或google scholar上面搜索到自己名字,感觉很nice,研究生的心愿算是完成 ...
- [原]openstack-kilo--issue(十六) instance can't get ip 虚拟机不能得到ip(1)
=====问题点:vm instance不能正常获取ip地址(此时用户是:admin) =======不一样的点:如果使用用户demo用户,启动一个vm,同样的image这个时候就能正确获取ip == ...