我知道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/