python - 是否可以在 Google Colab 上运行 Selenium?
问题描述
我在直接从 Google Colab 使用 Selenium 时遇到了困难。每当我在本地机器上运行下面的代码时,我都会成功,但是我想在 Colab 中测试相同的应用程序,但是我测试的所有选项都无法成功。
import warnings
warnings.filterwarnings('ignore')
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument('--ignore-certificate-errors-spki-list')
options.add_argument('--ignore-ssl-errors')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--disable-dev-shm-using")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument(r"user-data-dir=.\cookies\\test")
options.binary_location = '/usr/bin/google-chrome'
driver = webdriver.Chrome(options=options, executable_path='/usr/bin/chromedriver') #Error occurs in this function
driver.implicitly_wait(5)
print("Current session is {}".format(driver.session_id))
我还运行了以下命令:
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
尝试运行上述代码后,出现以下消息: WebDriverException: Message: unknown error: no chrome binary at /usr/bin/google-chrome
难道我做错了什么?
解决方案
我创建了一个库来帮助简化它。
!pip install kora
from kora.selenium import wd # web driver
print(wd.session_id) # 8be87366df11b09b552fb4ad7efbd696
推荐阅读
- django-rest-framework - Django REST 嵌套序列化器
- jenkins - 詹金斯:“无法初始化类 org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist”
- python - Python 3 如何打印 lambda 表达式?
- node.js - 如何使用nodejs替换csv文件
- powershell - 从 SharePoint Online 列表中删除所有项目
- google-drive-api - 是否可以使用从服务器端 Oauth2 获取的访问令牌打开谷歌选择器?
- linux - 可以使用 WSL2 构建 LFS
- keycloak - 令牌中的 KeyCloak 组 ID 丢失
- javascript - tf.compat.v1.image.resize 的等效 tensorflow.js 函数?
- r - 如何用spss文件中的data.frames中的标签替换值?