如何搭建Python3.4+Selenium

如题所述

详细步骤:
1.下载32位的python3.4 ,安装,注意在安装过程中,选择pip

2.添加系统环境变量

C:\Python34;C:\Python34\Scripts;C:\Program Files\Google\Chrome\Application(后面会用到)

3.使用pip命令,安装selenium
cd Python34
然后输入: pip install -U selenium
安装成功后,显示:
Runing setup.py install foe selenium
Successfully installed selenium-2.48.0

4.检测selenium是否安装成功。

在Python的IDLE中,输入:

from selenium import webdriver

如果没有报错,则selenium安装成功。

5.开始第一个程序

from selenium import webdriver

driver = webdriver.Chrome()

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

driver.find_element_by_id(“kw”).send_keys(“自动化测试”)

按F5运行,然后会自动打开百度网页。

能够顺利安装,那是幸运的,但是更多时候,总因为各种原因导致安装过程不太顺利。

下面将安装过程中遇到的问题,总结在此:

issue 1:

使用pip 命令安装结束后,加载selenium模块报错。
from selenium import webdriver

报错:

File “pyshell#0”, line 1, in

import selenium

File “C:\Python34\selenium.py”, line 1, in

from selenium import webdriver
ImportError: cannot import name ‘webdriver’
Traceback (most recent call last):

File “pyshell#0”, line 1, in

import selenium

File “C:\Python34\selenium.py”, line 1, in

from selenium import webdriver
百度了很多方法,仍无解。
解决方法: 最后换了版本,将64位python3.4 改为32位的python3.4
再次重新安装,成功。

issue 2:
使用browser=webdriver.Chorme() 报错。
报错:
Error message: “’chromedriver’ executable needs to be available in the path”
这是因为缺少驱动:Chromedriver.exe
下载Chormedriver.exe
注意安装Chromedriver.exe要与PC所安装的Chrome版本匹配。
解决方法:下载安装ChromeDrvier.exe 将ChromeDrvier.exe 放到C:\Python34\Scripts\
温馨提示:答案为网友推荐,仅供参考
相似回答