使用 Firefox headless、Selenium 和 Python 时出错
Error while using Firefox headless, Selenium and Python
我正在尝试使用 firefox headless、Selenium 框架和 Python 在 Amazon EC2 Ubuntu linux 上获取网页。我的代码如下所示:
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()
现在当我 运行 这个时,我得到以下错误:
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)
我已确保我的 geckodriver 和 firefox 版本兼容,我已尝试重新启动我的 EC2 实例,但没有任何效果。
感谢任何帮助。
试试这个 [使用 webdriver-manager]
pip install webdriver-manager
from webdriver_manager.firefox import GeckoDriverManager
self.browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())
它会自动修复您遇到的任何驱动程序错误
可以用吗
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(executable_path='path to the driver', options=options)
这是完整的工作代码,我在 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()
更新:这似乎是一个 OS 问题。当我使用 Amazon Linux 创建一个新的 EC2 实例时,相同的代码可以正常工作。旧的 EC2 实例 (Ubuntu) 仍然给我同样的错误。
2022 年使用服务对象的工作尝试:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()),options=options)
driver.get("https://www.google.com")
print('Done')
driver.quit()
我正在尝试使用 firefox headless、Selenium 框架和 Python 在 Amazon EC2 Ubuntu linux 上获取网页。我的代码如下所示:
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()
现在当我 运行 这个时,我得到以下错误:
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)
我已确保我的 geckodriver 和 firefox 版本兼容,我已尝试重新启动我的 EC2 实例,但没有任何效果。
感谢任何帮助。
试试这个 [使用 webdriver-manager]
pip install webdriver-manager
from webdriver_manager.firefox import GeckoDriverManager
self.browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())
它会自动修复您遇到的任何驱动程序错误
可以用吗
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(executable_path='path to the driver', options=options)
这是完整的工作代码,我在 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()
更新:这似乎是一个 OS 问题。当我使用 Amazon Linux 创建一个新的 EC2 实例时,相同的代码可以正常工作。旧的 EC2 实例 (Ubuntu) 仍然给我同样的错误。
2022 年使用服务对象的工作尝试:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()),options=options)
driver.get("https://www.google.com")
print('Done')
driver.quit()