如何使用Selenium定位并点击按钮?多次点击「Continuar >>」失败求助
Hey Guilherme, let's dig into why you're having trouble clicking that submit button. I spot a couple of potential issues with your current approach, plus some fixes that should get this working:
1. XPath Case Sensitivity Mismatch
Looking at your XPath, you're targeting a title attribute with:@title='Clique aqui para entrar na pesquisa'
But the actual HTML element has:
<input type="submit" class="basic" value=" Continuar » " title="Clique aqui para entrar na Pesquisa">
Notice the capitalized P in "Pesquisa" at the end? XPath is case-sensitive, so this tiny mismatch is likely causing your script to fail finding the element. That's probably the main culprit here!
2. Missing Explicit Waits
Web pages load content asynchronously—your script might be trying to click the button before it's fully rendered and interactive. Adding an explicit wait ensures the element is ready to be clicked before you attempt the action.
Fixed Code Examples
Here are a few reliable approaches to try:
Option 1: Correct the XPath with the Exact Title
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Firefox() driver.get('https://gru.inpi.gov.br/pePI/jsp/marcas/Pesquisa_classe_basica.jsp') # Wait up to 10 seconds for the button to be clickable, then click it wait = WebDriverWait(driver, 10) submit_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@type='submit' and @title='Clique aqui para entrar na Pesquisa']"))) submit_button.click()
Option 2: Target the Button's Unique Value Attribute
Since the button's value is specific to it, this can be a more stable locator:
# Use the exact value text (including spaces and the » character) submit_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@type='submit' and @value=' Continuar » ']"))) submit_button.click()
Option 3: Simplify with the Class Name
If the basic class is only used for this submit button, you can keep it even simpler:
submit_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "basic"))) submit_button.click()
Quick Additional Checks
- Ensure Firefox and GeckoDriver are on matching, up-to-date versions (mismatches often cause weird interaction issues).
- Double-check if the button is nested inside an iframe—if it is, you'll need to switch to the iframe first with
driver.switch_to.frame()before locating the element.
内容的提问来源于stack exchange,提问作者GuilhermeAraujo

