Selenium Python 浏览器=webdriver.Firefox() 错误

乔纳森

我正在尝试 selenium 的Simple Usage它的代码driver = webdriver.Firefox()给出了错误。

这是我的完整代码:

while True:
    try:
        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys
    except:
        print('Failed to import selenium tools, retrying...')
        continue
    else:
        print('Selenium import success!')
        break

while True:
    try:
        browser = webdriver.Firefox()
    except:
        print('Open browser error: An error occured, retrying..')
        continue
    else:
        print('Success!')
        break

browser.get('http://www.python.org')
assert "Python" in browser.title

elem = browser.find_element_by_name('q')
elem.send_keys('pycon')
elem.send_keys(Keys.RETURN)

assert "No results found" not in browser.page_source

结果:

Selenium import success!
Open browser error: An error occured, retrying..Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\code\python project\auto.py", line 14, in <module>
    browser = webdriver.Firefox()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 160, in __init__
    self.service.start()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\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: 'geckodriver' executable needs to be in PATH. 


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\code\python project\auto.py", line 16, in <module>
    print('Open browser error: An error occured, retrying..')

Selenium import success!结果表明,有在导入过程中没有问题。另一方面,browser = webdriver.Firefox()是造成问题的原因。

我应该如何更改它以使其正常工作?注意:我正在处理的文件名为 auto.py

风险

这里下载 Windows 版本的 Geckodriver并将其放在方便的地方。然后在初始化browser变量时,将完整路径传递给geckodriver.exe像这样:

browser = webdriver.Firefox(executable_path='enter_path_here')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Selenium WebDriver Java绑定清除浏览器Cookies

如何使用Selenium WebDriver + Java获取浏览器控制台错误消息?

使用Java使用Selenium WebDriver捕获浏览器日志

Selenium WebDriver Firefox错误-无法连接

我们可以在python selenium webdriver中缩放浏览器窗口吗?

在Chrome / Firefox浏览器的Webdriver中禁用Cookie

无法使用给定的URL打开Firefox浏览器-Selenium Webdriver&Java

Selenium-WebDriver如何使用javascript和firefox浏览器突出显示元素

使用Selenium WebDriver运行TOR浏览器

Selenium与PHP firefox浏览器问题

Python:新浏览器对象上的Selenium-chromedriver错误

使用Selenium Python和Firefox重新打开同一浏览器窗口

Selenium打开浏览器,但返回错误

尝试通过Selenium和Python使用GeckoDriver Firefox登录Gmail帐户时出现“此浏览器或应用程序可能不安全”错误

Python:Selenium Webdriver Chrome打开浏览器,但未打开所选站点

Python Selenium Webdriver:如果最小化浏览器,则不会向下滚动页面

PHP Selenium Webdriver读取浏览器输出

Safari浏览器:Selenium Webdriver异常

Selenium运行错误的浏览器(默认)

为什么Firefox是Webdriver的默认浏览器

根据selenium webdriver,浏览器的本机支持是什么

RedHat 6.8 上的 Selenium Webdriver + geckodriver + python 2.6.6 + Firefox 无法打开浏览器

无法使用 Selenium Webdriver 启动 IE 浏览器

如何使用 selenium WebDriver 和 python 打开功能齐全的 Chrome 浏览器?

如何让 selenium webdriver 停止打开浏览器?

Python selenium webdriver 什么都没有,但浏览器正常显示网页

我如何在谷歌浏览器中运行这个 python+ selenium webdriver 代码?

无法使用 selenium webdriver 启动 Chrome 浏览器

Selenium WebDriver 连接到 Kameleo 浏览器