【问题标题】:Newly created chrome profile won't load in新创建的 chrome 配置文件不会加载
【发布时间】:2020-04-11 21:35:57
【问题描述】:

当我运行这段代码时

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument(r"user-data-dir=C:\Users\micha\AppData\Local\Google\Chrome\User Data\Profile 1")

driver = webdriver.Chrome(executable_path=r'C:\Users\micha\Desktop\Visual_projects\chromedriver.exe', chrome_options = chrome_options)

driver.get("https://store.steampowered.com/")

这个错误弹出: [12216:1336:0411/232857.718:ERROR:browser_switcher_service.cc(238)] XXX Init()

有人可以帮助我吗?我不知道出了什么问题,但程序不会打开我创建的新配置文件。任何帮助将不胜感激。

我到处搜索如何修复此错误,但我认为指南已过时

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver


    【解决方案1】:

    不完全回答您的问题。但我发现这个link 非常有用。

    另外,我看到您正在尝试包含用户目录选项。实际上,这不是必需的,因为它会在启动 chromedriver.exe 时创建一个临时目录。

    了解chromedriver.exe - chromedriver.exe -h 的选项非常有帮助。

    请参阅下面的示例,这对我来说效果很好。另外,我更喜欢在窗口中使用没有空格的路径,这有助于保持简单。 如果您使用--verbose 而不是--log-level=INFO,您将获得所有日志。

    在 chromedriver 日志中,您可以看到为 chromedriver.exe 提供的默认参数。

    import time, os
    
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    exePath = './driver/chromedriver.exe'
    logPath = os.path.join(os.getcwd(),'logs','chromedriver.log')
    serviceArgs = ["--log-level=INFO", "--readable-timestamp", "--append-log"]
    
    # service = Service(executable_path=exePath, log_path=logPath, service_args=serviceArgs)
    service = Service(log_path=logPath, service_args=serviceArgs)
    service.start()
    print(service.service_url)
    print(service.process.pid)
    
    # driver = webdriver.Remote(service.service_url)
    # Update to remove infobars and save password popups
    
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    prefs = {"profile.password_manager_enabled": False, "credentials_enable_service": False}
    options.add_experimental_option("prefs", prefs)
    caps = options.to_capabilities()
    
    driver = webdriver.Remote(service.service_url, desired_capabilities=caps)
    
    # driver = webdriver.Remote('http://localhost:63404')
    driver.get('http://www.google.com/')
    driver.maximize_window()
    time.sleep(3) # Let the user actually see something!
    driver.get("https://github.com")
    time.sleep(3)
    driver.back()
    time.sleep(3)
    driver.close()
    driver.quit()
    service.stop()
    

    service.stop() 终止 chromedriver.exe 否则它将继续在后台运行。
    我最后的文件夹结构如下:

    rootDir
    --driver/chromedriver.exe
    --testchromedriver.py (with above code)
    

    另外,创建为killchromedriver.py 文件以终止所有chromedriver.exe 实例,以防它们在后台运行。

    import psutil # pip install psutil
    
    for process in psutil.process_iter():
        if(process.name() == 'chromedriver.exe'):
            process.terminate()
            print('chromedriver.exe was running and is now terminated')
    

    更新: 添加删除信息栏和保存密码弹出窗口的选项

    【讨论】:

    【解决方案2】:

    将我的 chrome 浏览器更新到 81 版,因此也必须更新 chrome webdriver。但是在更新之后它也开始给我这个错误。 这是代码,我已经从中删除了其他方法/功能。

    from selenium import  webdriver
    from time import sleep
    import sys
    sys.path.insert(1, '../..')
    from secrets import username, password
    
    class MyBot():
        def __init__(self):
            self.driver=webdriver.Chrome()
    
    bot=MyBot()
    

    [3220:14704:0416/145232.014:ERROR:browser_switcher_service.cc(238)] XXX Init()

    【讨论】:

    • 它也像[8840:13028:0417/081702.218:ERROR:browser_switcher_service.cc(238)] XXX Init()一样出现在我的情况下,但它不会阻止我运行测试。
    猜你喜欢
    • 2019-12-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多