[英]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.