新创建的chrome配置文件不会加载到中

2024-06-16 10:33:43 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行此代码时

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:错误:browser\u switcher\u service.cc(238)]XXX Init()

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

我到处寻找如何修复这个错误,但我认为指南已经过时了


Tags: fromimportadddriverselenium错误chromeargument
2条回答

不是你问题的确切答案。但是我发现这个非常有用

另外,我看到您正在尝试包括用户目录选项。实际上,这不是必需的,因为它在启动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')

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

将我的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:错误:browser\u switcher\u service.cc(238)]XXX Init()

相关问题 更多 >