You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python/Selenium调用二次driver.get()后如何保持登录状态?

Troubleshooting: Selenium Redirects Back to Login After Accessing Playlist URL

Hey there, let's break down why your Selenium script is bouncing back to the login page when accessing the playlist URL—even though manual access works perfectly. Here are the most likely causes and actionable fixes:

1. You’re Navigating Too Soon (Session Not Fully Established)

When you click the login button, the browser is still processing authentication requests, setting session cookies, and loading the post-login page. If you call driver.get() immediately after clicking, the server might not have finished registering your authenticated session yet, so it treats you as unauthenticated.

Fix: Wait for a Post-Login Confirmation Element

Instead of relying on vague implicit waits, explicitly wait for an element that only appears after a successful login (like a user profile icon, dashboard heading, or logout button). This ensures your session is fully active before navigating to the playlist.

Modify your code like this:

Login_Button.click()

# Wait for a post-login element (replace with a valid selector for CBT Nuggets)
wait = ui.WebDriverWait(driver, 20)
# Example: Wait for the user menu icon in the top-right corner
wait.until(lambda driver: driver.find_element_by_css_selector("button[data-testid='user-menu-button']"))

# Now navigate to the playlist safely
driver.get("https://www.cbtnuggets.com/learn/it-training/playlist/nrn:playlist:user:5fcf88f463ebba00155acb18/2?autostart=1")

Pro tip: Use your browser’s dev tools to inspect an element that loads right after login and grab its CSS selector or XPath.

2. Chrome Profile Issues (Session/Cookies Not Persisting)

Your user-data-dir might not point to your actual, active Chrome profile. When you manually browse, you’re using a profile with saved login cookies—but Selenium could be using a fresh or corrupted profile copy that doesn’t retain session data.

Fixes:

  • Double-check that userdata_path points to your real Chrome user profile directory (on Windows, it’s usually C:\Users\<YourName>\AppData\Local\Google\Chrome\User Data\Default).
  • Close all Chrome windows before running the script—if the profile is in use, Selenium might create a temporary copy that won’t save cookies.

3. Website Detected Selenium as an Automation Tool

Many platforms (including CBT Nuggets) detect Selenium’s default automation markers (like the webdriver flag) and restrict sessions for automated tools. This is why manual access works, but Selenium gets redirected.

Fix: Hide Selenium’s Automation Traces

Uncomment and adjust your Chrome options to mask automation signals:

options = webdriver.ChromeOptions()
options.add_argument(f"user-data-dir={userdata_path}")
# Add these lines to make the browser look like a regular manual session
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')

These tweaks reduce the chance of the site flagging your script as automated.

4. Alternative: Simulate Manual URL Navigation

Instead of using driver.get(), try mimicking the manual process of typing the URL into the address bar and pressing Enter. This can sometimes bypass session issues caused by direct get() calls:

# After confirming login success
# Option 1: Use JavaScript to navigate
driver.execute_script("window.location.href = 'https://www.cbtnuggets.com/learn/it-training/playlist/nrn:playlist:user:5fcf88f463ebba00155acb18/2?autostart=1';")

# Option 2: Simulate typing in the address bar
from selenium.webdriver.common.keys import Keys
address_bar = driver.find_element_by_css_selector("input[aria-label='Search or enter website name']")
address_bar.clear()
address_bar.send_keys("https://www.cbtnuggets.com/learn/it-training/playlist/nrn:playlist:user:5fcf88f463ebba00155acb18/2?autostart=1")
address_bar.send_keys(Keys.ENTER)

Why Manual Access Works

When you manually type the URL, your browser has already fully processed the login, set all necessary cookies, and established a stable authenticated session. Selenium’s automation flows move faster than human interaction, so it skips the wait time needed for the session to stabilize—or triggers anti-automation checks that manual browsing avoids.

内容的提问来源于stack exchange,提问作者Flying Thunder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:51:18