我如何在Jupyter Lab中为Selenium提供浏览器驱动程序



我在IBM的cognitiveclass Labs上使用selenium在jupyterlab中,尽管该软件包非常易于使用pip安装在此处(实际上我认为它已经预载了(,但它可以',但可以't在路径中找到所需的驱动程序:

from selenium import webdriver
browser = webdriver.Firefox()
[Out] FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
[Out] WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

对于所有其他浏览器也是一样的,只需用chrome/ie替换替换'geckodriver'。

无论如何,这可能是在真正的python安装上很简单的,但是如果可以的话,我更喜欢在jupyterlab中使用它。我下载了驱动程序.EXE文件,并将其放置在我项目的目录中,实验室将其视为/resources/myproj。然后,我将其添加到Jupyterlab已经使用的路径中,并指定了Selenium可执行文件的位置:

%env PATH=/home/jupyterlab/conda/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/resources/myproj
driver = os.path.normpath(os.path.join(os.getcwd(), 'geckodriver.exe'))
browser = webdriver.Firefox(executable_path=driver)
[Out] PermissionError: [Errno 13] Permission denied: '/resources/myproj/geckodriver.exe'
[Out] WebDriverException: Message: 'geckodriver.exe' executable may have wrong permissions.

我(或必须(可以在Jupyterlab中修改这些文件的权限,以便硒访问它们吗?还是有其他方法可以模仿Jupyter中的浏览器?

编辑: Corey Goldberg是对的,这是Linux环境而不是Windows环境,我能够将Linux驱动程序chmod chmod chmod 特殊问题。但是硒仍阻止我。

[Out] SessionNotCreatedException: Message: Unable to find a matching set of capabilities

我遇到的问题(这与Debanjanb引用的一个截然不同(是 Jupyterlab特定的

  • 它是否取决于我本地Windows机器的Firefox安装?它具有较旧的版本(52.5.2(,因此除了最新版本外,我还尝试了壁虎驱动程序(17.0(的相应版本。但是我怀疑,因为我没有将其指向Firefox在本地驱动器上的可执行文件。
  • 我需要32位还是64位Linux驱动程序?无论如何,我都尝试了两者,都返回上面的同一错误。但是我仍然不知道,因为从我的理解中,我的代码在IBM的计算机上运行,而不是我自己的计算机。

edit2:分辨率

这个问题可能针对我的工作环境。我使用jupyterlab的终端安装了最新的Firefox

$ cd /tmp
$ wget 'http://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US' -O firefox-67.0.4.tar.bz2
$ tar jxvf firefox-67.0.4.tar.bz2 -C /resources/myproj/

然后将Marionette功能设置为False,创建了一个明确的Firefox二进制文件,以便我可以查看日志(最终它没有写任何东西,不知道为什么(

(
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
driver = os.path.normpath(os.path.join(os.getcwd(), 'geckodriver'))
binary = os.path.normpath(os.path.join(os.getcwd(), 'firefox', 'firefox'))
ff_binary = webdriver.firefox.firefox_binary.FirefoxBinary(firefox_path=binary, log_file='ff_log.log')
browser = webdriver.Firefox(firefox_binary=ff_binary, capabilities=cap, executable_path=driver)
browser.get('http://google.com/')

最后,似乎我这一边停止了它,它已经超出了我最初的问题的范围。

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

geckodriver.exe是Windows的驱动程序。从路径的外观中,您需要Linux版本。一旦您不构建它,可执行文件将命名为 geckodriver(否.exe(。然后,您将需要运行chmod以在使用之前给予其可执行权限。

最新更新