首页 > 解决方案 > 为什么 Selenium chromedriver 不添加配置文件?

问题描述

我正在尝试将 chrome 配置文件添加到我的程序中,但它什么也没做。有人可以帮忙吗?

我正在使用 Selenium python。当我运行时,它似乎忽略了这些语句并在临时配置文件中运行。

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import random
import string
from selenium.webdriver.chrome.options import Options

while True:

        emailstarter = random.randint(11111111111111111111,999999999999999999999)
        first = random.randint(666666,888888)
        last = random.randint(555,55555)

        driver = webdriver.Chrome(r"C:\Program Files (x86)\chromedriver.exe")  # Optional argument, if not specified will search path.
        options = webdriver.ChromeOptions()
        options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default");


        

标签: pythonseleniumdebuggingselenium-chromedriver

解决方案


你的顺序错了。您应该首先创建选项,然后将它们作为参数传递给webdriver.Chrome类:

    from selenium import webdriver

    options = webdriver.ChromeOptions()
    options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
    driver = webdriver.Chrome(
        executable_path=r"C:\Program Files (x86)\chromedriver.exe",
        options=options
    )

推荐阅读