Selenium元素可定位但无法点击超时:Mohmal验证码获取失败
Hey there, let's break down why you're hitting this clickability issue on the Mohmal inbox page and fix it step by step. The error element located but not clickable usually means the element exists in the DOM but isn't interactable—here are the most likely fixes:
Common Causes & Solutions
1. Wait for the element to be clickable, not just present
Your current code uses presence_of_element_located, which only checks if the element exists in the DOM. But the element might still be hidden, loading, or covered by another element. Switch to element_to_be_clickable to ensure it's ready for interaction.
2. Don't click the <tbody> directly—target the actual email row
The <tbody> is just a container element, not a clickable item. You should click the first email row inside it instead (like tr[1]), since that's what users would normally click to open a message.
3. Ensure the element is in the viewport
Sometimes elements are off-screen, and Selenium can't click them until they're scrolled into view. Use JavaScript to scroll to the element before clicking.
4. Check for iframes (hidden content containers)
Some email services load inbox content inside an iframe. If Mohmal does this, you'll need to switch to the iframe first to access its contents.
Modified Code Implementation
Here's how to update your code with these fixes:
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time # ------------------- # Fix: Open the first email in inbox # ------------------- try: # Wait for the first email row to be clickable first_email = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//*[@id='inbox-table']/tbody/tr[1]")) ) # Scroll to the element if it's off-screen driver.execute_script("arguments[0].scrollIntoView(true);", first_email) # Click the email row to open it first_email.click() time.sleep(1) print("Opened the first email in the inbox") except Exception as e: print(f"Error opening email: {str(e)}") print("Failed to click the email") # ------------------- # Fix: Retrieve verification code # ------------------- vfCode = "" try: # Wait for the verification code element to be visible (not just present) vf_element = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, vfCode_CSS)) ) vfCode = vf_element.text.strip() # Strip extra whitespace from the code print(f"Verification code obtained: {vfCode}") print("Successfully copied verification code") except Exception as e: print(f"Error getting verification code: {str(e)}") print("Failed to copy verification code")
Additional Checks to Try
- Close overlays/ads: If there's a pop-up or ad covering the inbox, add code to close it first (e.g., locate a close button and click it).
- Verify element selectors: Mohmal might have updated its page structure. Use your browser's dev tools to recheck the XPath/CSS selectors for the email row and verification code.
- Switch to iframe (if needed): If the inbox is inside an iframe, add this before accessing inbox elements:
# Replace with the actual iframe selector (e.g., by ID or name) iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "inbox-iframe"))) driver.switch_to.frame(iframe) # ... perform inbox actions here ... # Switch back to the main page when done driver.switch_to.default_content()
内容的提问来源于stack exchange,提问作者Fatih Tüz

