selenium - 在 colab 中安装并运行 selenium 浏览器

标签 selenium google-chrome firefox google-colaboratory python-webbrowser

我知道google colab可以用于selenium模块,但是在使用selenium之前,colab虚拟机中应该有一个浏览器,所以我使用命令在colab中成功安装了firefox

!apt-get update
!apt install firefox

但是当我尝试使用命令运行 Firefox

!firefox

它抛出一个错误

src/tcmalloc.cc:283] Attempt to free invalid pointer 0x7f4e34915040 Redirecting call to abort() to mozalloc_abort

其他浏览器(例如 chromium-browser 和 chrome)也会出现同样的问题。
我什至尝试过

import webbrowser
webbrowser.get('firefox').open('https://www.youtube.com')

但它抛出一个错误说

could not found browser location

所以总的来说,我需要解决运行浏览器时发生的此错误

Attempt to free invalid pointer 0x7f4e34915040

最佳答案

我无法安装 Firefox。但如果你可以使用Chrome。这是代码。

# install chromium, its driver, and selenium
!apt install chromium-chromedriver
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.website.com")
print(wd.page_source)  # results
# divs = wd.find_elements_by_css_selector('div')

关于selenium - 在 colab 中安装并运行 selenium 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58042043/

相关文章:

ruby - 如何编写使用 chrome 的 ruby​​ 网络爬虫?

java - 如何找到下拉菜单中选定的选项?

selenium - 自动点击 chrome ://extensions page using selenium webdriver 上的按钮

google-chrome - 如何处理 Chrome Apps 中的自签名证书?

google-chrome - 类型错误 : Cannot read properties of null (reading 'CodeMirror' )

javascript - Firefox 在每次请求 Promise ajax 调用时弹出 "This web page is being redirected to a new location"- 有什么办法可以避免吗?

javascript - 为拖放的 selenium UI 集成测试执行 Javascript - Java

javascript - 我的 ASP.NET 网站在 Chrome 的任务栏上显示纸质图标

node.js - 如何使用 Webdriver 控制 Firefox 扩展的侧边栏

javascript - firefox 和 javascript 重定向