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

使用Selenium自动化登录afghanbids.com遭访问拒绝,求解决方法

Fixing "Access Denied" with Selenium on 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-chromedriver for 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 with pip 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:

    1. Manually log into afghanbids.com in Chrome.
    2. Open Chrome DevTools (F12), go to the Application tab, expand Cookies > https://afghanbids.com/.
    3. 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": "/"}]).
    4. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 15:02:33