【问题标题】:Cannot use "get" function to load site after opening chrome with active profile on Chrome with Selenium?在使用 Selenium 的 Chrome 上打开具有活动配置文件的 chrome 后,无法使用“get”功能加载站点?
【发布时间】:2021-10-08 18:17:55
【问题描述】:

我可以使用以下标志成功加载我的 chrome 配置文件: user-data-dirprofile-directory,但是一旦加载配置文件并且 chrome 窗口实际上是打开的,就不会出现任何网页。它只是卡在一个空白屏幕上。

当我删除配置文件的代码时,它实际上能够打开存储在login-url 变量中的网页。

尝试更新到最新版本的 chrome (94.0.4606.81),我还使用here 列出的确切步骤来确保我拥有正确的 chrome 驱动程序版本。

我还做了很明显的事情,比如确保没有任何 chrome 实例在后台运行。

代码如下:

import os
from os.path import exists
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

headless = False
login_url = "https://google.com)"

def startChrome():
    global headless
    try:
        chrome_options = Options()
        if headless:
            chrome_options.add_argument("--headless")
        chrome_options.add_argument("----user-data-dir=C:/Users/ERIK/AppData/Local/Google/Chrome/User Data")
        chrome_options.add_argument("--profile-directory=Profile 1")    

        global driver
        driver = webdriver.Chrome(path+"/chromedriver.exe", options=chrome_options)
    except:
        print("Failed to start Chrome!")
        input()
        exit()

startChrome()

driver.get(login_url)
input()

【问题讨论】:

  • 那里有一个错字... login_url = "google.com)" 可能会干扰 url 验证。

标签: python selenium google-chrome selenium-chromedriver


【解决方案1】:

以下成功为我打开 google.com。

硒版3.141.0

ChromeDriver 版本94.0.4606.61

Chrome 版94.0.4606.71

from os import path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def getDriver(profile_directory, headless = False):
    chrome_options = Options()
    if headless:
        chrome_options.add_argument("--headless")

    userDataDir = path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\User Data")
    chrome_options.add_argument(f"--user-data-dir={userDataDir}")
    chrome_options.add_argument(f"--profile-directory={profile_directory}")

    return webdriver.Chrome("./chromedriver.exe", options=chrome_options)

driver = getDriver("Profile 2")
driver.get("https://google.com")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 2016-11-19
    • 1970-01-01
    • 2014-11-04
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多