簡體   English   中英

可以在 Google Colab 上運行 Selenium 嗎?

[英]Is running Selenium on Google Colab possible?

我無法直接從 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

嘗試刪除此行

options.binary_location = '/usr/bin/google-chrome'

並查看它是否適用於默認二進制位置。 不要忘記安裝驅動程序

!apt-get update 
!apt install chromium-chromedriver
# refer following discussion
# https://stackoverflow.com/questions/56829470/selenium-google-colab-error-chromedriver-executable-needs-to-be-in-path
# install chromium, its driver, and selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM