首页 技术 正文
技术 2022年11月17日
0 收藏 645 点赞 4,861 浏览 3994 个字

 一、所需工具

  1、Python3.6安装包

  2、Selenium安装包(selenium-server-standalone-3.8),如果是Python3的话可以不用下载selenium压缩包,Python3有自带的,直接输入命令安装即可

  3、Chromedriver驱动(Chromedriver2.3)

  4、Firefox驱动(geckodriver0.18)

  5、Chrome浏览器(chrome v.59)

  6、Firefox浏览器(Firefox v.54)

  7、Pycharm

  二、工具安装

  1、安装Python,安装完设置环境变量(将Python安装路径放入path下)

  2、selenium安装:cmd首先进入E:\Python\Scripts目录下,然后输入pip install -U selenium,等待安装完成即可

  3、pip安装:将pip工具包解压到指定盘,cmd命令进入该盘下,输入命令python setup.py install;再切换到E:\Python\Scripts 目录下输入E:\Python\Scripts > easy_install pip

  4、setuptools安装:将setuptools工具包解压到指定盘里,进入cmd,执行python ez_install.py即可

  5、将下载好的Chromedriver和geckodriver驱动放到Python安装路径下

  注意:Chromedriver和geckodriver驱动一定要和电脑上的浏览器版本对应,否则代码运行会报错

  Chromedriver和chrome浏览器对应版本

Python3+Selenium环境配置

  代码:

  from selenium import webdriver

  import time

  # browser=webdriver.Chrome()

  browser=webdriver.Firefox()

  url=’http://www.baidu.com’

  #通过get方法获取当前url打印

  print(“now access %s” %(url))

  browser.get(url)

  time.sleep(2)

   browser.find_element_by_xpath(“//input[@id=’kw’]”).send_keys(‘Selenium’)

  browser.find_element_by_id(‘su’).click()

  # browser.find_element_by_css_selector(“input[value$=’下’]”)

  time.sleep(3)

  print(“成功搜索”)

  browser.quit()

  以上代码如果能正常运行说明环境配置成功

  PS:以下三个问题是安装过程中常遇到的坑

  遇到第一个坑:’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下

Python3+Selenium环境配置

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919