【问题标题】:How can we use Selenium Webdriver in colab.research.google.com?我们如何在 colab.research.google.com 中使用 Selenium Webdriver?
【发布时间】: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。
  • 这个链接也有同样的问题:stackoverflow.com/questions/54327654/…
  • 好像是7天前问的
  • @johnmich 你解决问题了吗?
  • @Dimanjan 嘿,我已经停止尝试这个了。用例被废弃,因此没有进一步探索。

标签: python selenium selenium-webdriver google-colaboratory


【解决方案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")

【讨论】:

  • 你对壁虎司机有这样的建议吗?
  • 您好,请问这个链接中的问题:stackoverflow.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://stackoverflow.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答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 2020-01-18
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      相关资源
      最近更新 更多