【问题标题】:Selenium The browser appears to have exited before we could connectSelenium 在我们连接之前浏览器似乎已经退出
【发布时间】:2021-11-14 19:36:25
【问题描述】:

我尝试制作一个机器人来查找来自 Ajax Amsterdamn 的 Bob Marley Tshirt 是否可用。

https://www.adidas.ch/de/ajax-3-jsy/GT9559.html

我能够让它运行(还添加了一个电报机器人来报告成功:

#!/usr/bin/python3
from selenium import webdriver
import requests

token="" # expunged for obvious reasons
chat="" # expunged for obvious reasons
message="Available"
fireFoxOptions = webdriver.FirefoxOptions()
fireFoxOptions.set_headless()
browser = webdriver.Firefox(firefox_options=fireFoxOptions)
browser.get("https://www.adidas.ch/de/ajax-3-jsy/GT9559.html")
if ("Dieses Produkt ist leider ausverkauft." not in browser.page_source):
    send_text = 'https://api.telegram.org/bot' + token + '/sendMessage?chat_id=' + chat + '&parse_mode=Markdown&text=' + message  
    response = requests.get(send_text)
    print(response.json())
browser.close()

工作:

  • 硒 3.141.0
  • 蟒蛇3
  • 火狐 91.0.2
  • geckodriver 0.29.1
  • 操作系统:Manjaro Linux

所以在那之后我尝试将它部署在我的 Debian 10 服务器上,但这就是斗争的开始。我必须安装 Firefox 78.14.0esr 并根据它的 Geckodriver 版本 0.27.0 的 github 发布页面。 Selenium 与 3.141.0 保持一致。据我所知和我研究的版本应该没问题,但是执行时会抛出这个令人神经紧张的错误:

Traceback (most recent call last):
  File "./ajax.py", line 18, in <module>
    browser = webdriver.Firefox(options=options) #, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

我搜索了错误,显然你必须定义二进制路径和能力“木偶”现在我这样做了,现在代码看起来像这样(包括一些调试内容):

#!/usr/bin/python3
from selenium import webdriver
import requests
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

#token="1995953311:AAH-D7S-MISkCa0yQxUc84Gf978fz0vtoqY"
#chat="1917512203"
message="just a test"

options = FirefoxOptions()
options.add_argument("--headless")
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
binary = "/usr/bin/firefox"
options.binary = binary
browser = webdriver.Firefox(options=options, capabilities=cap, executable_path="/usr/local/bin/geckodriver")

browser.get("https://www.adidas.ch/de/ajax-3-jsy/GT9559.html")
if ("Dieses Produkt ist leider ausverkauft." not in browser.page_source):
    send_text = 'https://api.telegram.org/bot' + token + '/sendMessage?chat_id=' + chat + '&parse_mode=Markdown&text=' + message  
    response = requests.get(send_text)
    print(response.json())
else:
    print("succ")
browser.close()

但现在我收到以下错误:

Traceback (most recent call last):
  File "./ajax.py", line 18, in <module>
    browser = webdriver.Firefox(options=options, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 191, in __init__
    self.binary, timeout)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
    self.binary.launch_browser(self.profile, timeout=timeout)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 73, in launch_browser
    self._wait_until_connectable(timeout=timeout)
  File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 104, in _wait_until_connectable
    "The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

还将cap["marionette"] = False 更改为True 只会给我较旧的错误消息。

谢谢!

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver firefox


    【解决方案1】:

    我刚刚恢复了所有更改,制作了一个 Docker 容器并将其放在服务器上。

    【讨论】:

      猜你喜欢
      • 2014-11-22
      • 2015-01-31
      • 2016-10-16
      • 1970-01-01
      • 2013-09-20
      • 2019-02-18
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多