这不是转发
我正在使用 Linux 并且不能创建新的配置文件。我想像selenium gui 一样加载现有的配置文件(而不是创建新的配置文件)。
我能够让铬发挥作用,但不能让谷歌浏览器发挥作用。Chrome 会打开,但会返回
selenium.common.exceptions.WebDriverException: Message: Service /opt/google/chrome/chrome unexpectedly exited. Status code was: 0
错误。
我正在尝试使用用户目录访问权限启动 google chrome,以便捕获现有会话。
失败的代码:
option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
有效但启动铬而不是谷歌铬的代码:
option.add_argument("user-data-dir=/home/user/snap/chromium/common/.cache/chromium/Default/") #)PATH is path to your>
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
我确定我使用的是正确的可执行文件
我很确定我安装了正确的 chromedriver 驱动程序
root@Inspiron-laptop:/home/user# pip3 install chromedriver-autoinstaller
Requirement already satisfied: chromedriver-autoinstaller in /usr/local/lib/python3.8/dist-packages (0.2.2)
只是使用不当。
如何在访问缓存目录时从 selenium 中启动 google-chrome?
我在 Ubuntu 20.04
更新:
完整脚本:
#!/usr/bin/python3
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from seleniumbase import BaseCase
from selenium.webdriver.chrome.options import Options
import time
import random
minptime = 25
maxptime = 120
class MyweClass(BaseCase):
def method_a():
option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")
option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
driver.get("https://world.com/myworld")
print(f'driver.command_executor._url: {driver.command_executor._url}')
print(f'driver.session_id: {driver.session_id}')
time.sleep(18)
return driver
driver = MyweClass.method_a()
更新二:
使用相同的错误
option.add_argument("user-data-dir=~/.config/google-chrome/Default/")
和
driver = webdriver.Chrome('/opt/google/chrome/google-chrome', options=option)
和
chmod -R 777 /home/user/.config
确保用户以用户身份访问缓存目录。
谷歌浏览器信息:

