WebDriverException:Message:进程意外关闭,状态信号|如何在Google Colboratory中运行Selenium Firefox Geckodriver? [英] WebDriverException: Message: Process unexpectedly closed with status signal | How to run Selenium Firefox Geckodriver in Google Colaboratory?

查看:25
本文介绍了WebDriverException:Message:进程意外关闭,状态信号|如何在Google Colboratory中运行Selenium Firefox Geckodriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿Stackoverflow社区,我想用Google recolab中的Firefox geckodriver为python运行Selenium。我最近找到了一个关于这个话题的帖子,里面有一个来自前段时间@DebanjanB的回答:https://stackoverflow.com/a/57553986/15163882。遗憾的是,提供的解决方案不再起作用,我想不出如何修复它。

这是堆栈溢出答案中的代码,应该下载并运行geckodriver;

# install firefox, geckodriver, and selenium
!apt-get update
!pip install selenium
!apt install firefox-geckodriver
!cp /usr/lib/geckodriver /usr/bin
!cp /usr/lib/firefox /usr/bin

from selenium import webdriver

binary = '/usr/bin/firefox'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument('start-maximized')
options.add_argument('--headless')
browser = webdriver.Firefox(firefox_options=options, executable_path='/usr/bin/geckodriver')
browser.get('http://google.com/')

!cp /usr/lib/geckodriver /usr/bin导致错误,没有这样的文件,geckodriver存储在/usr/bin/geckodriver中。!cp /usr/lib/firefox /usr/bin也不起作用,/usr/lib/firefox是目录,不是文件。总体而言,代码失败并出现异常:Process unexpectedly closed with status signal

以下是完整的错误输出:

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy, executable_path, options, service_log_path, firefox_options, service_args, desired_capabilities, log_path, keep_alive)
    172                 command_executor=executor,
    173                 desired_capabilities=capabilities,
--> 174                 keep_alive=True)
    175 
    176         # Selenium remote

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    155             warnings.warn("Please use FirefoxOptions to set browser profile",
    156                           DeprecationWarning, stacklevel=2)
--> 157         self.start_session(capabilities, browser_profile)
    158         self._switch_to = SwitchTo(self)
    159         self._mobile = Mobile(self)

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    250         parameters = {"capabilities": w3c_caps,
    251                       "desiredCapabilities": capabilities}
--> 252         response = self.execute(Command.NEW_SESSION, parameters)
    253         if 'sessionId' not in response:
    254             response = response['value']

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: Process unexpectedly closed with status signal

更新#1

这是我最大的努力,它将所有内容放在所需的所有路径中:

import sys,os
from os import system as o

!pip install selenium

def sudo_cmd(cmd):o('echo %s|sudo -S %s' % ('12345678', cmd))
sudo_cmd('apt-get update')

sudo_cmd('apt install firefox-geckodriver')
sudo_cmd('cp /usr/lib/geckodriver /usr/bin')
sys.path.insert(0,'/usr/lib/geckodriver')

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.headless = True
options.add_argument('--no-sandbox')
driver = webdriver.Firefox(options=options, executable_path='geckodriver')
driver.get('https://www.google.com/')

但我仍然收到相同的错误。

我还注意到geckodriver.log文件在安装代码错误之前输出src/tcmalloc.cc:283] Attempt to free invalid pointer 0x7fa48e915160 Redirecting call to abort() to mozalloc_abort

远程服务器正在Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic上运行。

如果有人能帮我,我会很高兴的!👍

推荐答案

出现相同错误。经过一些实验,找到了这样的解决方案:

!apt-get update
!pip install selenium
!apt install firefox-geckodriver
!cp /usr/lib/geckodriver /usr/bin
!cp /usr/lib/firefox /usr/bin

t = '''
from selenium import webdriver

binary = '/usr/bin/firefox'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument('start-maximized')
options.add_argument('--headless')
browser = webdriver.Firefox(options=options, executable_path='/usr/bin/geckodriver')
browser.get('http://google.com/')
print(browser.page_source)

'''
with open("main.py", 'w') as file:
  file.write(t)

!sudo python3 main.py


这篇关于WebDriverException:Message:进程意外关闭,状态信号|如何在Google Colboratory中运行Selenium Firefox Geckodriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆