码迷,mamicode.com
首页 > 编程语言 > 详细

python selenium自动化登录错误解决

时间:2017-11-15 17:06:47      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:span   浏览器   download   coding   stderr   ace   top   line   指定   

from selenium import webdriver
browser = webdriver.Firefox()

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

 错误代码

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:\Python34\lib\subprocess.py", line 859, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Python项目/numpy/function_args.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "C:\Python34\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 144, in __init__
    self.service.start()
  File "C:\Python34\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. 

  网上找到很多方法添加路径

from selenium import webdriver
browser = webdriver.Firefox("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")

添加PATH环境变量,还是报错,

后来在stackoverflow找到答案

https://stackoverflow.com/questions/40437226/selenium-wont-work-with-firefox-or-chrome

Firefox和Chrome,需要下载geckodriver / chromedriver。这些驱动程序是您安装的浏览器与selenium之间进行通信所必需的。所以你需要:

firefox驱动下载地址: https://github.com/mozilla/geckodriver/releases

chrome驱动下载地址:  https://sites.google.com/a/chromium.org/chromedriver/downloads

1.  安装python(pip install selenium)selenium

2. 下载你使用的浏览器的驱动程序(chromedriver,geckodriver,operadriver等)

将所下载的驱动geckodriver.exe文件路径添加到代码

谷歌  : driver = webdriver.Chrome(executable_path=‘/path/to/chromedriver.exe‘)

火狐:   driver = webdriver.Firefox(executable_path=‘/opt/geckoDriver/geckodriver.exe‘)

完整代码:

from selenium import webdriver

#browser = webdriver.Firefox()
browser = webdriver.Firefox(executable_path=rC:\Users\Administrator\Desktop\bs\geckodriver.exe)
browser.get(http://www.baidu.com/)
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), geckodriver))
binary = FirefoxBinary(rC:\Program Files (x86)\Mozilla Firefox\firefox.exe)
browser = webdriver.Firefox(firefox_binary=binary,executable_path=gecko+.exe)
browser.get(http:///www.google.com)
browser.close()

 

python selenium自动化登录错误解决

标签:span   浏览器   download   coding   stderr   ace   top   line   指定   

原文地址:http://www.cnblogs.com/kali-aotu/p/7839577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!