脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - selenium3.0+python之环境搭建的方法步骤

selenium3.0+python之环境搭建的方法步骤

2021-08-31 00:47inside802 Python

这篇文章主要介绍了selenium3.0+python之环境搭建的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文目标:
使用selenium3.0+python3操纵浏览器,打开百度网站。(相当于selenium的hello world)

环境基础:python3已安装,pycharm编辑器已安装。

第一步:安装selenium

打开cmd窗口,输入pip install selenium,然后回车。

selenium3.0+python之环境搭建的方法步骤

第二步:安装WebDriver

1)下载WebDriver
由于selenium是通过调用浏览器的给的接口来操纵浏览器,所以WebDriver不统一,需要根据自己的浏览器登录相应的地址,下载最新的WebDriver

Chrome:https://chromedriver.storage.googleapis.com/index.html

FireFox:https://github.com/mozilla/geckodriver/releases

Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/(注意:微软官方已不对IE浏览器的webdriver进行维护)

Safari: 不用下载WebDriver,该浏览器已内置接口。

selenium3.0+python之环境搭建的方法步骤

我下载了Edge和Chrome的webdriver。火狐的下载地址太卡了,下载不下来,我自己用火狐也不多。

2)将WebDriver放在环境变量里
selenium能够通过系统的path变量找到webdriver,因此需要将webdriver所在目录添加到系统的path变量里。
右键点击“此电脑”,然后选择属性,然后根据下图将webdriver所在的目录添加到Path变量里。

selenium3.0+python之环境搭建的方法步骤

第三步:编写代码,操纵浏览器访问百度网站并搜索

1)操纵谷歌浏览器访问百度

  1. from selenium.webdriver import Chrome
  2. from time import sleep
  3. with Chrome() as driver:
  4. driver.get('http://www.baidu.com')
  5. sleep(10)

2)操纵Edge访问百度

  1. from selenium.webdriver import Edge
  2. from time import sleep
  3. with Edge(executable_path=r'C:\WebDriver\msedgedriver.exe') as driver:
  4. driver.get('http://www.baidu.com')
  5. sleep(10)

Edge()函数里指定executable_path的值,是因为selenium仍然认为Edge的WebDriver的名称是MicrosoftWebDriver.exe,然而最新的Edge的WebDriver的名称是msedgedriver.exe。因此,需要手动指定Edge WebDriver的路径,否则,selenium会报错:selenium.common.exceptions.WebDriverException: Message: ‘MicrosoftWebDriver.exe' executable needs to be in PATH.

到此这篇关于selenium3.0+python之环境搭建的方法步骤的文章就介绍到这了,更多相关python selenium3.0环境搭建 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/inside802/article/details/113406690

延伸 · 阅读

精彩推荐