Selenium安装遇到的问题

1、Selenium安装

pip3 install selenium chromedriver_installer

2、编辑我的第一个测试脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""A very basic selenium example."""

from selenium import webdriver

browser = webdriver.Chrome()

browser.get("http://www.baidu.com")

browser.find_element_by_id("kw").send_keys("selenium")

browser.find_element_by_id("su").click()

browser.quit()

3、首次运行报错

调用chrome报错信息:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 756, in __init__
restore_signals, start_new_session)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1499, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test1.py", line 8, in <module>
browser = webdriver.Chrome()
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

意思是说没有找到chromedriver,或者不在PATH中。

换成调用Firefox也会有类似的报错信息:

FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'

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

在网上查了一下,有很多人都遇到了这个问题,这确实是学习selenium的第一个拦路虎,第一关就过不去。

4、解决办法

直接用brew安装chromedriver

brew install chromedriver

等了半天没有反应,因为chrome是那谁家的,在这里克服第二关,你会看到:

提示:
Error: No available formula with the name "chromedriver" 
It was migrated from homebrew/core to homebrew/cask.
You can access it again by running:
  brew tap homebrew/cask
And then you can install it by running:
  brew cask install chromedriver

按照提示再次输入:

brew tap homebrew/cask

brew cask install chromedriver
chromedriver was successfully installed!

搞定了Chrome,再装Firefox所需的geckodriver

brew install geckodriver

5、运行通过

再次运行脚本,可以正常的看到chrome打开了百度,执行了搜索,最后关闭了浏览器。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇文章在介绍官网的同时使用了比较多的脚本示例,示例里遇到的问题有部分在本篇文章进行了解释,还有一篇文章专门记录了...
    顾顾314阅读 14,418评论 3 32
  • 现状 好久好久没有更新博客了,应该有一个月了吧,这段时间内,好忙,公司的业务在上涨期,但是却把下面的一个小朋友砍掉...
    jb2阅读 5,090评论 0 2
  • Selenium 官网Selenium WebDriver官网webdriver实用指南python版本 WebD...
    顾顾314阅读 47,120评论 0 34
  • 洞见SELENIUM自动化测试 写在最前面:目前自动化测试并不属于新鲜的事物,或者说自动化测试的各种方法论已经层出...
    厲铆兄阅读 11,691评论 3 47
  • Selenium是一款强大的基于浏览器的开源自动化测试工具,最初由 Jason Huggins 于 2004 年在...
    FifiZhuang阅读 12,273评论 5 71