Selenium+Xpath+Expected Condition通过属性定位元素超时问题
data-category Element Let's break down why your code is timing out and fix it—starting with the most obvious issue first.
1. Your XPath Has a Critical Typo
Looking at your target element:
<div class='nav-subnav' data-category='drugstore'>
But your XPath uses @an-attribute='data-category'—that's not the actual attribute name! You're referencing a non-existent attribute instead of the real data-category attribute. That's definitely why Selenium can't find the element.
Corrected Code
Here's the fixed version with the right XPath:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC # Option 1: Target the data-category attribute directly getByAttribute = WebDriverWait(amazonDriver, 10).until( EC.visibility_of_element_located((By.XPATH, "//div[@data-category='drugstore']")) ) # Option 2: Make it more precise by combining class and data-category (better for avoiding duplicates) getByAttribute = WebDriverWait(amazonDriver, 10).until( EC.visibility_of_element_located((By.XPATH, "//div[@class='nav-subnav' and @data-category='drugstore']")) )
2. Check for Iframes (A Common Amazon Quirk)
Amazon sometimes loads navigation elements inside iframes. If the nav-subnav div is nested in an iframe, Selenium can't access it until you switch to that iframe first.
How to Handle Iframes
Add this code before your wait block to switch to the correct iframe (you might need to adjust the iframe selector if there are multiple):
# Wait for the iframe to be present, then switch to it iframe = WebDriverWait(amazonDriver, 10).until( EC.presence_of_element_located((By.TAG_NAME, "iframe")) ) amazonDriver.switch_to.frame(iframe) # Now run your element locator code here... # After interacting with elements inside the iframe, switch back to the main content if needed # amazonDriver.switch_to.default_content()
3. Adjust the Wait Condition (If Visibility Isn't the Right Check)
Sometimes elements are present in the DOM but not immediately visible. If visibility is still causing timeouts, try using presence_of_element_located instead—it waits for the element to exist in the DOM, not necessarily be visible:
getByAttribute = WebDriverWait(amazonDriver, 10).until( EC.presence_of_element_located((By.XPATH, "//div[@class='nav-subnav' and @data-category='drugstore']")) )
4. Ensure the Page is Fully Loaded
Amazon's pages are heavily dynamic. Add a wait for the document to be fully loaded before trying to locate elements:
WebDriverWait(amazonDriver, 15).until( lambda driver: driver.execute_script("return document.readyState") == "complete" )
Start with fixing the XPath typo first—that's 90% likely to be the root cause here. If it still times out, work through the other steps one by one.
内容的提问来源于stack exchange,提问作者cloudscape-germany

