Selenium Python中No Such Element Exception问题求助:无法定位Archionweb网站验证码按钮以提取邮箱
Hey there! I’ve run into similar issues with dynamic elements and anti-scraping measures on sites like this, so let’s break down how to fix that "no such element" error and get to the email.
Common Reasons You’re Seeing the Error & Fixes
1. The Verification Button is Inside an iframe
Many sites load verification widgets inside iframes to isolate them. Selenium looks for elements in the main DOM by default, so you need to switch to the iframe first:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://www.archionweb.be/Public/Company/1930") # Wait for the iframe to load and switch to it try: iframe = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, "iframe")) # Or use ID/name if available ) driver.switch_to.frame(iframe) except: # If no iframe exists, skip this step pass
2. The Element Loads Dynamically (AJAX)
Don’t try to click the button immediately after page load—wait for it to become clickable with explicit waits:
# Wait for the verification button to be clickable verify_button = WebDriverWait(driver, 15).until( EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'verify-email-btn')]")) # Replace with your actual selector ) # Scroll to the button first (in case it's off-screen) driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", verify_button) verify_button.click()
3. Your Selector Uses Dynamic Attributes
Check the button’s HTML in DevTools (F12)—if the id or class changes on each load (e.g., random strings), use stable attributes instead:
- Use text:
//button[text()='Show Email'](adjust to the actual button text) - Use partial class names:
//button[contains(@class, 'email-verify')] - Use parent/child relationships:
//div[@class='email-container']/button
4. The Site Detects Selenium as a Bot
Some sites flag automated browsers. Add these options to make your driver look more like a real user:
options = webdriver.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") 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") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options)
After Clicking the Button: Extract the Email
Once the button is clicked, switch back to the main DOM if you were in an iframe, then wait for the email element to appear:
# Switch back to main document if you used an iframe driver.switch_to.default_content() # Wait for and extract the email email_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, "span.email-address")) # Replace with your selector ) print("Extracted Email:", email_element.text) driver.quit()
Important Note on Captchas
If the "verification" is actually a full captcha (like reCAPTCHA), automated clicking won’t work. In that case, you’d need to use a captcha-solving service (ensure this complies with the site’s Terms of Service) or manually solve it for small-scale scraping.
内容的提问来源于stack exchange,提问作者NoobDev

