Pycharm+Python3+Selenium3 环境搭建

一、安装python

下载链接: 
Pycharm: 
https://www.jetbrains.com/pycharm/download/#section=windows 

Python3: 
https://www.python.org/downloads/release/python-365/ 
或者从Python官网下载最新Python3 

环境搭建步骤: 
准备工作: 
先通过下载链接准备好需要的Pycharm 和 Python,这里选择的Pycharm是Community版本;Python下载的是Python3版本。都是exe的安装包,安装非常简便。

(pycharm安装:

1、双击安装Pycharm,安装过程中需要破解;

打开激活窗口

选择 Activate new license with License server (用license server 激活)

在 License sever address 处填入 http://xidea.online

点击 Activate 进行认证

完成!)

Python3的安装(当前是3.x版本,勾选时记得勾选Path环境

直接点击下一步就可以完成安装,安装完成后,还需要改一下环境变量。因为我装在C盘下,所以在环境变量中的path中配置C:\Python3\,可以把这个环境变量加在Path的最前面。

检测Python是否安装成功,在cmd命令窗口中,输入Python,点回车即可。这样就安装成功啦! )

  • 安装驱动

驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases 

谷歌驱动版本要和谷歌版本相一致,且驱动只有32位  下载地址: 
https://blog.csdn.net/cz9025/article/details/70160273/

解压到python的安装目录,python的安装目录已配置到环境变量,所有不需要将驱动地址加到环境变量,否则需要添加

驱动位于安装目录下 :

  • 安装selenium3.0
  1. cmd输入:pip install selenium

pip版本升级,可以使用‘python -m pip install -U pip’来更新

2、使用命令”pip install setuptools安装setuptools

3、安装成功后,你将会在Python的安装目录下看到一个Scripts的文件夹

4、继续在环境变量中配置path,;C:\Python3\Script

5、在Python安装目录下,输入”easy_install pip”,点回车

       cd C:\Program Files\Python37  进入python安装目录

6、在Python安装目录下,输入”pip install -U selenium”,点回车  

这样就安装好Selenium了。

安装完成后可以在设置中设置python的路径。 

测试脚本

1.chromedriver

from selenium import webdriver

 

driver = webdriver.Chrome()

driver.get("http://www.baidu.com")

time.sleep(3)

driver.quit()

2.firefoxdriver

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get("http://www.python.org")

assert "Python" in driver.title

elem = driver.find_element_by_name("q")

elem.clear()

elem.send_keys("pycon")

elem.send_keys(Keys.RETURN)

assert "No results found." not in driver.page_source

driver.close()

五、遇到第一个坑:'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

https://images2015.cnblogs.com/blog/1070438/201703/1070438-20170319170732104-2018491945.png

 

七、遇到第三坑: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以上的版本,升级版本就可以了

https://images2015.cnblogs.com/blog/1070438/201703/1070438-20170319170819276-741436010.png

 

 

 


版权声明:本文为u011563656原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。