python+selenium环境搭建

1.Python安装

(1)官网下载:https://www.python.org/downloads/

(安装时记得修改安装路径)

(2)安装后在path中加入两个路径:

一是python.exe所在目录:F:\soft\Python\Python38-32

二是pip模块所在目录:F:\soft\Python\Python38-32\Scripts

安装后在命令执行窗口输入python,查看安装的python是否有信息:

2.Selenium环境搭建

2.1安装selenium模块

以管理员身份进入命令执行窗口

方法1.退出python执行命令pip install selenium

方法2.退出python执行命令python -m pip install selenium

2.2. 安装浏览器驱动

chrome driver:http://chromedriver.storage.googleapis.com/index.html

firefox driver(geckodriver):https://github.com/mozilla/geckodriver/releases/

edge driver:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

下载驱动后,可统一存放到python.exe所在的目录;如果是放到其他目录,请将路径加入系统环境变量path中;当然也可以在脚本中定义。

注:edge driver的安装,如果是发行版18及其以上的需通过管理员打开cmd运行命令安装(如下图框住的部分),安装后无需再进行路径等设置即可用。命令是DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0

2.3测试打开浏览器

#启动火狐浏览器

from selenium import webdriver

driver = webdriver.Firefox()

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

driver.find_element_by_id('kw').send_keys('selenium')

#启动谷歌浏览器

from selenium import webdriver

driver = webdriver.Chrome()

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

driver.find_element_by_id('kw').send_keys('selenium')

#启动edge浏览器

from selenium import webdriver

driver = webdriver.Edge()

driver.get('https://www.baidu.com')

2.4. selenium IDE下载安装

谷歌

下载地址:https://www.crx4chrome.com/crx/107238/

下载后的文件添加到谷歌扩展程序中,如果出现提示程序无效的情况,则将文件先修改后缀名为zip,然后解压,解压后才添加到谷歌的扩展程序中即可。

火狐

用火狐访问https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

点击添加。


IDE的使用请查看文章:https://www.jianshu.com/p/5885fd5540d8

3.Pycharm安装

安装包下载官网:https://www.jetbrains.com/pycharm/download/#section=windows

(安装时记得修改安装路径)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容