python selenium自动化登录错误解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python selenium自动化登录错误解决相关的知识,希望对你有一定的参考价值。
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=r‘C:\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(r‘C:\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自动化登录错误解决的主要内容,如果未能解决你的问题,请参考以下文章
python+selenium-12306实战解决登录滑块问题
一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试
用python+selenium进行一次cnblog的登录测试
selenium.common.exceptions.InvalidSessionIdException通过Python在无头模式下使用GeckoDriver Selenium Firefox(示例代