在无头覆盆子pi上用firefox运行python selenium

时间:2018-01-18 03:30:56

标签: python linux selenium firefox raspberry-pi

我正在尝试使用firefox让我的无头覆盆子pi上的selenium。我在带有chrome的Windows上运行良好。这是我的版本:

uname -a Linux megabyte.thompco.com 4.9.59-v7+ #1047 SMP Sun Oct 29
12:19:23 GMT 2017 armv7l GNU/Linux

which firefox
/usr/bin/firefox

firefox --version
Mozilla Firefox 52.5.2

./geckodriver_32 --version
geckodriver 0.19.1

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

我认为我有兼容版本的驱动程序和firefox(这似乎有效):

./geckodriver_32 -b /usr/bin/firefox
1516245181824   geckodriver     INFO    geckodriver 0.19.1
1516245181881   geckodriver     INFO    Listening on 127.0.0.1:4444

当我运行以下代码时:

def __init__(self, tag, user_name, password, driver_location, headless):
    logger = logging_utils.get_logger()
    logging_utils.start_function(logger, user_name=user_name)
    self.tag = tag
    self.user_name = user_name
    self.password = password
    self.cards = []
    driver_options = Options()
    driver = None
    try:
        if "chrome" in driver_location.lower():
            if headless:
                driver_options.add_argument("--headless")
            driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver.exe"),
                                      chrome_options=driver_options)
        elif "gecko" in driver_location.lower():
            binary = FirefoxBinary("/usr/bin/firefox")
            driver_options.binary = binary
            profile = webdriver.FirefoxProfile()
            driver_options.profile = profile
            driver_options.set_headless(headless)
            driver = webdriver.Firefox(firefox_binary=binary,
                                       firefox_profile=profile,
                                       executable_path=os.path.abspath(driver_location),
                                       firefox_options=driver_options)

我收到此错误:

Traceback (most recent call last):

  File "/mnt/usbdrive/python/AmexOfferChecker/amexParser.py", line 105, in __init__
    firefox_options=driver_options)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__
    keep_alive=True)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)

SessionNotCreatedException: Message: Unable to find a matching set of capabilities

欢迎任何建议!

我修改了我的“gecko”部分,如下所示:

            options = Options()
            options.add_argument('-headless')
            print driver_location
            print os.path.abspath(driver_location)
            driver = Firefox(executable_path=os.path.abspath(driver_location),
                             firefox_options=options)
            print "Driver has been loaded!"

现在我收到了这个错误:

  geckodriver_32
  /mnt/usbdrive/python/AmexOfferChecker/geckodriver_32
  Traceback (most recent call last):

  File "/mnt/usbdrive/python/AmexOfferChecker/amexParser.py", line 106, in __init__
    firefox_options=options)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__
    keep_alive=True)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)

  File "/home/jordan/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)

WebDriverException: Message: Process unexpectedly closed with status: 1

我确实看到了这个错误: 在Linux上,即使无头模式不使用它们,您的系统上也需要某些库 - 因为Firefox链接它们。有关详细信息和修复进度,请参阅bug 1372998。 我添加了建议的库:

sudo apt-get install libgtk-3-0 libdbus-glib-1-2 xvfb

但我仍然得到同样的例外。

我也尝试了同样的错误(注意创建了调试文件,但是空的):

            options = Options()
            options.add_argument('--headless')
            profile = webdriver.FirefoxProfile()   
            profile.set_preference("logs/webdriver.log", "/tmp/firefox_console")
            binary = FirefoxBinary(firefox_path="/usr/bin/firefox",
                                   log_file=open("/tmp/firefox_output", "wb"))
            driver = webdriver.Firefox(firefox_profile=profile,
                                       firefox_binary=binary,
                                       options=options,
                                       executable_path=os.path.abspath(driver_location))

最后为seleniuim开启了日志记录(这对任何人都意味着什么?):

2018-01-24 22:51:00,078 - selenium.webdriver.remote.remote_connection 480 -DEBUG - POST http://127.0.0.1:45413/session {"capabilities": {"alwaysMatch": {"acceptInsecureCerts": true, "browserName": "firefox", "moz:firefoxOptions": {"args": ["headless"]}}, "firstMatch": [{}]}, "desiredCapabilities": {"acceptInsecureCerts": true, "browserName": "firefox", "moz:firefoxOptions": {"args": ["headless"]}}}

2018-01-24 22:51:00,944 - selenium.webdriver.remote.remote_connection 567 -DEBUG - Finished Request {"value":{"error":"unknown error","message":"Process unexpectedly closed with status: 1","stacktrace":"stack backtrace:\n   0:   0x55d797 - backtrace::backtrace::trace::hc4bd56a2f176de7e\n   1:   0x55d8ff - backtrace::capture::Backtrace::new::he3b2a15d39027c46\n   2:   0x4b7f4b - webdriver::error::WebDriverError::new::ha0fbd6d1a1131b43\n   3:   0x4bcb57 - geckodriver::marionette::MarionetteHandler::create_connection::hf0532ddb9e159684\n   4:   0x4a14cb - <webdriver::server::Dispatcher<T, U>>::run::h2119c674d7b88193\n   5:   0x47fcbf - std::sys_common::backtrace::__rust_begin_short_backtrace::h21d98a9ff86d4c25\n   6:   0x4871cf - std::panicking::try::do_call::h5cff0c9b18cfdbba\n   7:   0x606237 - panic_unwind::__rust_maybe_catch_panic\n                at /checkout/src/libpanic_unwind/lib.rs:99\n   8:   0x4999e7 - <F as alloc::boxed::FnBox<A>>::call_box::h413eb1d9d9f1c473\n   9:   0x6000d3 - alloc::boxed::{{impl}}::call_once<(),()>\n                at /checkout/src/liballoc/boxed.rs:692\n                 - std::sys_common::thread::start_thread\n                at /checkout/src/libstd/sys_common/thread.rs:21\n                 - std::sys::imp::thread::{{impl}}::new::thread_start\n                at /checkout/src/libstd/sys/unix/thread.rs:84"}}
2018-01-24 22:51:00,947 - main.main 38 -WARNING - Problem (Message: Process unexpectedly closed with status: 1

这是错误的关键(有趣的是,它被报告为DEBUG)。有没有人有任何建议:

2018-01-24 22:51:02,863 - selenium.webdriver.remote.remote_connection 567 -DEBUG - Finished Request 
{"value":
    {"error":"unknown error","message":"Process unexpectedly closed with status: 1","stacktrace":"stack backtrace:
    0:   0x576797 - backtrace::backtrace::trace::hc4bd56a2f176de7e
    1:   0x5768ff - backtrace::capture::Backtrace::new::he3b2a15d39027c46
    2:   0x4d0f4b - webdriver::error::WebDriverError::new::ha0fbd6d1a1131b43
    3:   0x4d5b57 - geckodriver::marionette::MarionetteHandler::create_connection::hf0532ddb9e159684
    4:   0x4ba4cb - <webdriver::server::Dispatcher<T, U>>::run::h2119c674d7b88193
    5:   0x498cbf - std::sys_common::backtrace::__rust_begin_short_backtrace::h21d98a9ff86d4c25
    6:   0x4a01cf - std::panicking::try::do_call::h5cff0c9b18cfdbba
    7:   0x61f237 - panic_unwind::__rust_maybe_catch_panic
    at /checkout/src/libpanic_unwind/lib.rs:99
    8:   0x4b29e7 - <F as alloc::boxed::FnBox<A>>::call_box::h413eb1d9d9f1c473
    9:   0x6190d3 - alloc::boxed::{{impl}}::call_once<(),()>
    at /checkout/src/liballoc/boxed.rs:692
    - std::sys_common::thread::start_thread
    at /checkout/src/libstd/sys_common/thread.rs:21
    - std::sys::imp::thread::{{impl}}::new::thread_start
    at /checkout/src/libstd/sys/unix/thread.rs:84"
    }
}

2 个答案:

答案 0 :(得分:2)

最后得到了这个,但是使用了镀铬和镀铬。 您必须先安装chrome:

time                 temperature
----------           -------------
2018-01-01T00:00:00Z 95
2018-01-02T00:00:00Z 97
2018-01-03T00:00:00Z 102
2018-01-04T00:00:00Z 88
2018-01-05T00:00:00Z 94
2018-01-06T00:00:00Z 81
2018-01-07T00:00:00Z 71
2018-01-08T00:00:00Z 98
2018-01-09T00:00:00Z 127
2018-01-10T00:00:00Z 126

接下来从这里下载了debian软件包: https://packages.debian.org/stretch/armhf/chromium-driver/download 解压缩文件“chromedriver”:

sudo apt-get install chromium-browser

其余代码不变。注意

mkdir tmp
dpkg-deb -R chromium-driver_63.0.3239.84-1_deb9u1_armhf.deb tmp
cp /usr/local/bin/chromedriver .
mv chromedriver chromedriver_arm_64

很好(“ - 无头”可能也可以 - 我还没试过)。我肯定希望有人在花费尽可能多的时间之前发现这一点!

答案 1 :(得分:0)

您也可以尝试声明DISPLAY变量,它尤其适用于远程连接。 在终端上运行此命令:

export DISPLAY=:0.0