如何用Selenium点击非按钮/锚点的More元素以抓取OTCMarkets股票代码变更表格?
Got it, let's figure out why your click isn't landing and get that extra table data loaded. The main issues here are likely an incorrect XPath syntax, the element not being fully interactable when you try to click it, or the fact that it's a <div> instead of a standard button/link throwing Selenium off. Here's how to fix each scenario:
1. Fix Your XPath Expression
Your original XPath has syntax errors—let's correct that first. The valid way to target a div containing "More" text is:
# Correct XPath syntax more_element = driver.find_element(By.XPATH, "//div[contains(text(), 'More')]")
Your original code missed the double slashes (//) to start the path, and used invalid syntax inside contains() (it should be contains(text(), 'More') not contains(text()='More')). For even better precision, you can combine the class attribute from the HTML you shared to avoid matching other divs with "More" text:
# More precise XPath using the class attribute more_element = driver.find_element(By.XPATH, "//div[@class='_2sFaw3zGf1' and contains(text(), 'More')]")
2. Wait for the Element to Be Interactable
Dynamic pages often load elements after the initial page load, so trying to click immediately might fail. Use Selenium's explicit wait to ensure the element is ready to be clicked:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Wait up to 10 seconds for the element to become clickable wait = WebDriverWait(driver, 10) more_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='_2sFaw3zGf1' and contains(text(), 'More')]"))) more_button.click()
This waits until the element is visible and enabled, which prevents click failures due to unloaded elements.
3. Fallback: Use JavaScript to Trigger the Click
If the standard click() method still fails (sometimes divs don't behave like buttons for Selenium), you can use JavaScript to directly trigger the click event:
more_element = driver.find_element(By.XPATH, "//div[@class='_2sFaw3zGf1' and contains(text(), 'More')]") driver.execute_script("arguments[0].click();", more_element)
This bypasses Selenium's interactability checks and directly fires the click event on the element, which works for most non-standard clickable elements.
Try these steps in order—start with fixing the XPath, add the explicit wait, and use the JavaScript click if needed. That should get you past the "More" element and load the additional symbol change data.
内容的提问来源于stack exchange,提问作者AmilaMGunawardana

