Chrome успешно запускается. Это подтверждает аргументы-USER-DATA-DIR и--POFILE-DIRECTORY указывают на правильный профиль. Браузер остается на Chrome: // newtab/page.
Я очень усерден в обеспечении завершения всех процессов Chrome.exe (проверяется с помощью менеджера задач) перед запуском сценария.
:
ОС: Windows 11 Pro, версия 23H2
Selenium Версия: 4.25.0 < /p>
Chrome Browser Версия: 136.0.7103.93 (официальная сборка) (64-битная) < /p>
chromedriver version: 136.0.7103.xx (скачанный с официального "Chrome testing". Major.minor.build) < /p>
Упрощенный фрагмент кода: < /p>
Код: Выделить всё
import time
import os # For path checks
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException
# --- Configuration ---
# User needs to replace CHROME_DRIVER_PATH with the full path to their chromedriver.exe
CHROME_DRIVER_PATH = r'C:\path\to\your\chromedriver-win64\chromedriver.exe'
# User needs to replace YourUserName with their actual Windows username
CHROME_PROFILE_USER_DATA_DIR = r'C:\Users\YourUserName\AppData\Local\Google\Chrome\User Data'
CHROME_PROFILE_DIRECTORY_NAME = "Default" # Using the standard default profile
TARGET_URL = "https://aistudio.google.com/prompts/new_chat" # Example
def setup_driver():
print(f"Driver Path: {CHROME_DRIVER_PATH}")
print(f"User Data Dir: {CHROME_PROFILE_USER_DATA_DIR}")
print(f"Profile Dir Name: {CHROME_PROFILE_DIRECTORY_NAME}")
if not os.path.exists(CHROME_DRIVER_PATH):
print(f"FATAL: ChromeDriver not found at '{CHROME_DRIVER_PATH}'.")
return None
if not os.path.isdir(CHROME_PROFILE_USER_DATA_DIR):
print(f"FATAL: Chrome User Data dir not found at '{CHROME_PROFILE_USER_DATA_DIR}'.")
return None
chrome_options = Options()
chrome_options.add_argument(f"--user-data-dir={CHROME_PROFILE_USER_DATA_DIR}")
chrome_options.add_argument(f"--profile-directory={CHROME_PROFILE_DIRECTORY_NAME}")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation", "load-extension"])
chrome_options.add_experimental_option('useAutomationExtension', False)
# chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Tried with and without
chrome_options.add_argument("--start-maximized")
try:
service = Service(executable_path=CHROME_DRIVER_PATH)
driver = webdriver.Chrome(service=service, options=chrome_options)
print("WebDriver initialized.")
return driver
except WebDriverException as e:
print(f"FATAL WebDriverException during setup: {e}")
if "user data directory is already in use" in str(e).lower():
print(">>> Ensure ALL Chrome instances are closed via Task Manager.")
return None
except Exception as e_setup:
print(f"Unexpected FATAL error during setup: {e_setup}")
return None
def main():
print("IMPORTANT: Ensure ALL Google Chrome instances are FULLY CLOSED before running this script.")
input("Press Enter to confirm and continue...")
driver = setup_driver()
if not driver:
print("Driver setup failed. Exiting.")
return
try:
print(f"Browser launched. Waiting a few seconds for it to settle...")
print(f"Initial URL: '{driver.current_url}', Initial Title: '{driver.title}'")
time.sleep(4) # Increased wait after launch for profile to fully 'settle'
print(f"Attempting to navigate to: {TARGET_URL}")
driver.get(TARGET_URL)
print(f"Called driver.get(). Waiting for navigation...")
time.sleep(7) # Increased wait after .get() for navigation attempt
current_url_after_get = driver.current_url
current_title_after_get = driver.title
print(f"After 7s wait - Current URL: '{current_url_after_get}', Title: '{current_title_after_get}'")
if TARGET_URL not in current_url_after_get:
print(f"NAVIGATION FAILED: Browser did not navigate to '{TARGET_URL}'. It's still on '{current_url_after_get}'.")
# Could also try JavaScript navigation here for more info
# print("Attempting JavaScript navigation as a fallback test...")
# driver.execute_script(f"window.location.href='{TARGET_URL}';")
# time.sleep(7)
# print(f"After JS nav attempt - URL: '{driver.current_url}', Title: '{driver.title}'")
else:
print(f"NAVIGATION SUCCESSFUL to: {current_url_after_get}")
except Exception as e:
print(f"An error occurred during main execution: {e}")
finally:
print("Script execution finished or errored.")
input("Browser will remain open for inspection. Press Enter to close...")
if driver:
driver.quit()
if __name__ == "__main__":
# Remind user to update paths if placeholders are detected
if r"C:\path\to\your\chromedriver-win64\chromedriver.exe" == CHROME_DRIVER_PATH or \
r"C:\Users\YourUserName\AppData\Local\Google\Chrome\User Data" == CHROME_PROFILE_USER_DATA_DIR:
print("ERROR: Default placeholder paths are still in the script.")
print("Please update CHROME_DRIVER_PATH and CHROME_PROFILE_USER_DATA_DIR with your actual system paths.")
else:
main()
< /code>
Консоль (когда он застрял на новой вкладке): < /p>
Setting up Chrome driver from: C:\Users\stat\Downloads\chromedriver-win64\chromedriver-win64\chromedriver.exe
Attempting to use Chrome User Data directory: C:\Users\stat\AppData\Local\Google\Chrome\User Data
Attempting to use Chrome Profile directory name: Default
WebDriver initialized successfully.
Browser launched. Waiting a few seconds for it to settle...
Initial URL: 'chrome://newtab/', Initial Title: '新分頁'
DevTools remote debugging requires a non-default data directory. Specify this using --user-data-dir.
[... some GCM / fm_registration_token_uploader errors may appear here ...]
Attempting to navigate to: https://aistudio.google.com/prompts/new_chat
Called driver.get(). Waiting for navigation...
After 7s wait - Current URL: 'chrome://newtab/', Title: '新分頁'
NAVIGATION FAILED: Browser did not navigate to 'https://aistudio.google.com/prompts/new_chat'. It's still on 'chrome://newtab/'.
версия Chromedriver точно соответствует версии Chrome Browser.minor.minor.build (136.0.7103).
Все процессы Chrome.exe завершаются через менеджер задачи перед выполнением скрипта. Chrome_driver_path, chrome_profile_user_data_dir и chrome_profile_directory_name правильны для моей системы.
Браузер видно, что мой профиль «по умолчанию» (показывает мою тему, новая вкладка). /> Удаленная отладка Devtools требует не деко-по умолчании «Предупреждение», как и некоторые ошибки GCM, но сам браузер открывается с профилем. Driver.get () не удается уйти от Chrome: // newtab/? Существуют ли конкретные варианты хрома для Selenium 4.25+ или известные проблемы с Chrome 136, которые могут вызвать такое поведение при использовании существующего, богатого профиля пользователя, даже если Chrome полностью закрыт заранее? Как я могу надежно сделать Driver.get () иметь приоритет над новой страницей вкладок по умолчанию, загружающей в этом сценарии?
Подробнее здесь: https://stackoverflow.com/questions/796 ... ead-of-nav