使用Selenium自动化登录afghanbids.com遭访问拒绝,求解决方法
Hey there! That "Access Denied" error is almost certainly because the site is detecting your Selenium instance as an automated bot. Websites use various checks to spot bots, but we can tweak our setup to make the browser session look far more legitimate. Let's go through the most effective fixes:
Modify Chrome options to hide automation traces
Chrome enables default flags that scream "automated browser" to detection systems. Add these options to your driver setup to mask those traces:from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # Disable the flag that marks the browser as automation-controlled chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Mimic a real user-agent string (update this to match your current Chrome version if needed) chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36") # Disable extensions and infobars that give away automation chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-infobars") # Start the browser maximized, like a real user would chrome_options.add_argument("--start-maximized") driver = webdriver.Chrome(options=chrome_options) driver.get("https://afghanbids.com/")Use
undetected-chromedriverfor better stealth
This is a modified ChromeDriver built specifically to bypass bot detection. It handles most stealth tweaks automatically, so it's often the quickest fix. First install it withpip install undetected-chromedriver, then use it like this:import undetected_chromedriver as uc driver = uc.Chrome() driver.get("https://afghanbids.com/")Add human-like delays
Real users don't load a page and act instantly. Add small waits to mimic natural browsing behavior. For reliability, use Selenium's explicit waits instead of hard-coded sleeps:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver.get("https://afghanbids.com/") # Wait until the page's body loads (adjust the selector if needed for this site) WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))Import cookies from a manual session (optional)
If the site uses cookies to track legitimate users, exporting cookies from your manually logged-in Chrome session and importing them into Selenium can make your session look like a returning user:- Manually log into afghanbids.com in Chrome.
- Open Chrome DevTools (F12), go to the Application tab, expand Cookies >
https://afghanbids.com/. - Copy each cookie's name, value, domain, and path into a JSON file (example structure:
[{"name": "cookie_name", "value": "cookie_value", "domain": "afghanbids.com", "path": "/"}]). - Import them into Selenium:
import json with open("cookies.json", "r") as f: cookies = json.load(f) driver.get("https://afghanbids.com/") for cookie in cookies: driver.add_cookie(cookie) driver.refresh()
Avoid old headless mode (if using headless)
The original headless Chrome is easy to detect. If you need headless browsing, use Chrome's newer headless mode (available in Chrome 112+):chrome_options.add_argument("--headless=new")
Start with undetected-chromedriver—it's usually the most effective fix. If that doesn't work, combine it with the user-agent and delay tweaks for extra stealth.
内容的提问来源于stack exchange,提问作者Talib Daryabi

