简体   繁体   English

使用 Firefox 无头、Selenium 和 Python 时出错

[英]Error while using Firefox headless, Selenium and Python

I am trying to use firefox headless, Selenium framework and Python to fetch webpage on Amazon EC2 Ubuntu linux. I am trying to use firefox headless, Selenium framework and Python to fetch webpage on Amazon EC2 Ubuntu linux. My code looks like this:我的代码如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True

driver = webdriver.Firefox(options=options,executable_path='/home/ubuntu/geckodriver')
driver.get("https://google.com")
print('Done')
driver.quit()

Now when I run this, I get the following error:现在,当我运行它时,出现以下错误:

Traceback (most recent call last):
  File "test1.py", line 7, in <module>
    driver = webdriver.Firefox(options=options,executable_path='/home/ubuntu/geckodriver')
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Connection refused (os error 111)

I have made sure that my geckodriver and firefox versions are compatible, I have tried rebooting my EC2 instance but nothing is working.我已确保我的 geckodriver 和 firefox 版本兼容,我已尝试重新启动我的 EC2 实例,但没有任何效果。

Any help is appreciated.任何帮助表示赞赏。

try this [with webdriver-manager ]试试这个 [与 webdriver-manager ]

pip install webdriver-manager 
from webdriver_manager.firefox import GeckoDriverManager
self.browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())

and it will automatically fix any driver error you have它会自动修复您遇到的任何驱动程序错误

Could that be used.可以这样用吗。

from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(executable_path='path to the driver', options=options)

This is the complete working code, I tested it on Windows machine with Pycharm community edition IDE这是完整的工作代码,我在 Windows 机器上用 Pycharm 社区版 IDE 测试了它

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
options = Options()
options.headless = True
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),firefox_options=options)
driver.get("https://google.com")
print('Done')
driver.quit()

Update: It seems like it was an OS issue.更新:这似乎是一个操作系统问题。 When I created a new EC2 instance using Amazon Linux, same code is working without issue.当我使用 Amazon Linux 创建一个新的 EC2 实例时,相同的代码可以正常工作。 The older EC2 instance (Ubuntu) is still giving me the same error.较旧的 EC2 实例(Ubuntu)仍然给我同样的错误。

You need to pass the complete url including the www .您需要通过完整的 url 包括www So instead of:所以而不是:

driver.get("https://google.com")

You need:你需要:

driver.get("https://www.google.com/")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python 3在无头模式下运行firefox浏览器时出现错误 - Getting error while running firefox browser in headless mode using python 3 Python Selenium 无头 state 时发生错误? - Python Selenium error occurs while in headless state? Firefox Python硒不会变得无所适从 - Firefox Python selenium not going headless 504 网关超时错误使用 Firefox 68.9.0esr 在无头模式下使用 GeckoDriver Selenium 和 Python - 504 Gateway Time-out error using Firefox 68.9.0esr in headless mode with GeckoDriver Selenium and Python 在Python的Selenium webdriver中使用带有无头firefox的http代理 - Using a http proxy with headless firefox in Selenium webdriver in Python 在无头火狐(Ubuntu)上使用 selenium 使用 python 下载文件 - Downloading files with python using selenium on headless firefox (Ubuntu) 如何在使用 Selenium 和 Python 执行测试后关闭 Headless Firefox - How to close Headless Firefox after test execution using Selenium and Python AttributeError: &#39;NoneType&#39; 对象在通过 Python 使用 Selenium 使用无头 Chrome 截取屏幕截图时没有属性 &#39;encode&#39; 错误 - AttributeError: 'NoneType' object has no attribute 'encode' error while taking screenshot with headless Chrome using Selenium through Python 如何使用 Python 在 Selenium 中以编程方式使 Firefox 无头? - How to make Firefox headless programmatically in Selenium with Python? 在python中使用Selenium在无头chrome中下载文件时文件未保存 - File Not Saving While Downloading File in Headless chrome using Selenium in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM