搭建Selenium环境

1.下载并安装Python

此学习笔记使用Python语言进行开发,所以我已经安装了Python环境,我的Python版本为3.5.2:

2.安装selenium

因为我使用的Python3版本,在该版本中已经包含了pip,使用pip安装selenium,因为我本机已经安装了selenium,因此会出现如下提示:

Pip 的常用命令如下。

> pip install selenium==3.11.0 # 安装指定版本号
> pip install -U selenium # 安装最新版本号
> pip show selenium # 查看当前包的版本信息
> pip uninstall selenium # 卸载Selenium

3.安装selenium开发环境

本学习笔记我使用PyCharm工具进行学习:

4.编写selenium程序

使用selenium编写访问http://www.baidu.com的程序:

1 # -*- coding:utf-8 -*-
2 
3 from selenium import webdriver
4 
5 driver = webdriver.Firefox()
6 driver.get("http://www.baidu.com")

执行程序:

1 selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

报错了,此时我们需要下载Firefox浏览器的geckodriver.exe驱动,然后放到Python的安装目录或者随便放一个目录中,把该目录设置到Path环境变量,我的安装目录为:D:Python35,然后再次执行程序。

可以看到,可以正确使用Firefox访问百度。

原文地址:https://www.cnblogs.com/zhuzhaoli/p/10432582.html