如何使用Selenium Python定位LOAD MORE按钮?UFC运动员页面爬取按钮定位求助
Hey Nick, let's work through that tricky "Load More" button targeting issue on the UFC athletes page. Based on the HTML snippet you shared, here are several reliable approaches to locate that element:
Class + Text Content Combination
The button has abuttonclass and the exact text "Load More" — combining these two traits makes for a precise locator. For example, in Selenium:driver.find_element(By.XPATH, "//a[@class='button' and text()='Load More']")
If you're worried about extra whitespace or line breaks messing with exact text matches, use a contains check instead:driver.find_element(By.XPATH, "//a[@class='button' and contains(text(), 'Load More')]")Leverage the
relAttribute
Notice the button hasrel="next"— this attribute is standard for pagination "next" actions, so it's a great target. You can use either XPath or CSS selectors:- XPath:
driver.find_element(By.XPATH, "//a[@rel='next']") - CSS Selector:
driver.find_element(By.CSS_SELECTOR, "a.button[rel='next']")
- XPath:
Href Parameter Filter
The button's href includes apage=parameter, which you can use to narrow down the element:driver.find_element(By.XPATH, "//a[@class='button' and contains(@href, 'page=')]")
Quick Troubleshooting Tips
Don't forget these common pitfalls that might cause your locator to fail:
- Dynamic Loading: The button might not appear immediately, so use explicit waits to ensure it's clickable before interacting. Here's a Selenium example:
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) load_more_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button' and text()='Load More']"))) - Iframe/Shadow DOM Check: Confirm the button isn't nested inside an iframe or shadow DOM. If it is, you'll need to switch contexts first before locating the element.
内容的提问来源于stack exchange,提问作者Nick_Lopez43

