在Google Colab中安装Chrome并运行Twitch观众脚本时遭遇Chrome无法正常启动的问题求助
在Google Colab中安装Chrome并运行Twitch观众脚本时遭遇Chrome无法正常启动的问题求助
嘿,我仔细看了你的问题和脚本,发现核心问题出在你用了Windows本地的Chrome路径——但Colab是运行在云端Ubuntu服务器上的,根本找不到D:\Program Files\Google\Chrome\Application\chrome.exe这个路径,这就是Chrome启动失败的关键原因!
下面给你整理了完整的修正方案:
1. 正确的Chrome(Chromium)安装步骤
Colab的Ubuntu环境对Chromium(Chrome的开源适配版)支持更好,你的安装命令有些重复,整理成正确的执行顺序:
!apt-get update !apt-get install -y chromium-chromedriver !pip install selenium colorama pystyle
2. 修正后的完整Python脚本
主要改掉本地路径依赖,适配Colab的云端环境,同时移除没用的Windows命令:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from colorama import Fore from pystyle import Center, Colors, Colorate # 适配Colab Ubuntu环境的Chrome配置 options = webdriver.ChromeOptions() options.add_argument("--disable-extensions") options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") # Colab必须的参数,避免权限问题 options.add_argument("--disable-dev-shm-usage") # 解决内存不足问题 options.add_argument("--remote-debugging-port=9222") options.add_argument("--headless=new") # 新版无头模式,兼容性更强 options.binary_location = "/usr/bin/chromium-browser" # 指定Colab中Chromium的安装路径 # 指定Chromedriver的路径 service = Service("/usr/lib/chromium-browser/chromedriver") twitch_username = input(Colorate.Vertical(Colors.green_to_blue, ("Enter your channel name (e.g Kichi779): "))) proxy_count = int(input(Colorate.Vertical(Colors.cyan_to_blue, ("How many proxy sites do you want to open? (Viewer to send)")))) # 初始化浏览器驱动 driver = webdriver.Chrome(service=service, options=options) # 打开第一个代理窗口访问Twitch driver.get('https://www.blockaway.net') text_box = driver.find_element(By.ID, 'url') text_box.send_keys(f'www.twitch.tv/{twitch_username}') text_box.send_keys(Keys.RETURN) # 循环打开多个窗口 for i in range(proxy_count): driver.execute_script("window.open('https://www.blockaway.net/')") driver.switch_to.window(driver.window_handles[-1]) text_box = driver.find_element(By.ID, 'url') text_box.send_keys(f'www.twitch.tv/{twitch_username}') text_box.send_keys(Keys.RETURN) input(Colorate.Vertical(Colors.red_to_blue, ("Viewers have all been sent. You can press enter to close it..."))) # 最后关闭驱动,释放资源 driver.quit()
3. 关键修正点说明
- 移除了Windows本地的
chrome_path和driver_path,替换为Colab环境中Chromium和Chromedriver的实际路径 - 把旧的
--headless参数换成--headless=new,这是Chrome官方推荐的新版无头模式,稳定性更高 - 删除了
os.system(f"title ...")命令,因为Colab终端不支持Windows的窗口标题设置 - 最后添加了
driver.quit(),避免云端资源泄漏
按照这个方案执行,应该就能正常启动浏览器并运行你的Twitch观众脚本了!如果还有报错,可以查看Colab终端的具体提示,通常会明确指出问题所在。
备注:内容来源于stack exchange,提问作者MrBaati
相关产品推荐
相关产品推荐

