我们如何在 colab.research.google.com 中使用 Selenium Webdriver?

Posted

技术标签:

【中文标题】我们如何在 colab.research.google.com 中使用 Selenium Webdriver?【英文标题】:How can we use Selenium Webdriver in colab.research.google.com? 【发布时间】:2018-12-05 09:15:23 【问题描述】:

我想在 colab.research.google.com 中使用 Chrome 的 Selenium Webdriver 进行快速处理。我可以使用!pip install selenium 安装 Selenium,但 chrome 的 webdriver 需要 webdriverChrome.exe 的路径。我应该如何使用它?

P.S.- colab.research.google.com 是一个在线平台,它为与深度学习相关的快速计算问题提供 GPU。请避免使用 webdriver.Chrome(path) 等解决方案。

【问题讨论】:

我想我提到了“colab.research.google.com”。我知道 webdriver 如何在本地机器上工作。但是由于 colab research google 是一个为快速机器学习处理问题提供 GPU 的在线平台,所以我想在上面提到的这个在线平台上使用 webdrive。 这个链接也有同样的问题:***.com/questions/54327654/… 好像是7天前问的 @johnmich 你解决问题了吗? @Dimanjan 嘿,我已经停止尝试这个了。用例被废弃,因此没有进一步探索。 【参考方案1】:

您可以通过安装 chromium webdriver 并调整一些选项使其不会在 google colab 中崩溃:

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.get("https://www.webite-url.com")

【讨论】:

你对壁虎司机有这样的建议吗? 您好,请问这个链接中的问题:***.com/questions/54327654/… 感谢分享,我会试试这个,让你知道 cp: cannot stat '/usr/lib/chromium-browser/chromedriver': 没有这样的文件或目录 此解决方案自 21 年 6 月 4 日起生效。谢谢!【参考方案2】:

这个在 colab 工作过

!pip install selenium
!apt-get update 
!apt install chromium-chromedriver

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

【讨论】:

【参考方案3】:

我创建了自己的库以方便使用。

!pip install kora -q
from kora.selenium import wd
wd.get("https://www.website.com")

【讨论】:

这太棒了。非常感谢。 在 google colab 上不起作用:给出相同的错误:WebDriverException:消息:服务 chromedriver 意外退出。状态码是:-6 @AhmadM。它现在应该可以工作了。最初的 URL 不正确。 确实,这是一个很棒的图书馆! @korakot 你的库中是否支持 Tor 浏览器?【参考方案4】:

没有足够的声望来发表评论。 :(

不过,@Thomas 的回答在 06.10.2021 中仍然有效,但只需一个简单的更改,您就会得到DeprecationWarning: use options instead of chrome_options

下面的工作代码:

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://***.com/questions/51046454/how-can-we-use-selenium-webdriver-in-colab-research-google-com")
wd.title

【讨论】:

如果您没有发表评论的声誉,您应该改为编辑 q/a 并写下答案,直到您获得声誉为止。 完全正确,因此这是一个答案;) 我的意思是写出对其他问题的实际答案,这实际上不是cmets。【参考方案5】:

要在 GOOGLE COLAB 中使用 selenium,请在 colab 笔记本中执行后续步骤

!pip install kora -q

如何在 COLAB 中使用它:

from kora.selenium import wd
wd.get("enter any website here")

你也可以用它来做美丽的汤

import bs4 as soup
wd.get("enter any website here")
html = soup.BeautifulSoup(wd.page_source)

【讨论】:

【参考方案6】:

您可以通过使用 WebDriverManager 来摆脱使用 .exe 文件,而不是这个

System.setProperty("webdriver.gecko.driver", "driverpath/.exe");
WebDriver driver = new FirefoxDriver();

你会写这个

WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();

您需要的只是将依赖项添加到 POM 文件中(我假设您使用 maven 或一些构建工具) 请在此链接中查看有关如何使用它的完整答案 Using WebdriverManager

【讨论】:

您在 Colab 上运行 python,而不是 javascript。请给出python答案。

以上是关于我们如何在 colab.research.google.com 中使用 Selenium Webdriver?的主要内容,如果未能解决你的问题,请参考以下文章

我们如何在php中处理动态表单

async/await 抛出 NullReferenceException 我们如何诊断我们在哪里搞砸了?

我们如何在 Prometheus 度量系列中添加我们自己的时间戳?

我们如何在 Apache Spark 中执行动态重新分区?

如何判断我们是不是在主线程中运行?

我们如何在 agora 应用中对远程流进行情感分析?