如何用选择器爬取网页?解决ogimet无value属性年份选项点击问题
Hey Javier, I’ve dealt with exactly this kind of dropdown issue in Selenium before—when options don’t have a value attribute, you just need to target them by their visible text instead. Here are a couple of straightforward approaches that work reliably:
1. Use Selenium's Select Class (Recommended)
The Select class is built specifically for handling dropdown menus, and it makes selecting options by text super easy—no need to mess with raw XPATH/CSS selectors. Here's how to integrate it into your code:
First, import the Select class at the top of your script:
from selenium.webdriver.support.ui import Select
Then, once you're on the page with the year dropdown, locate the dropdown element and use select_by_visible_text():
# Wait for the dropdown to load (you can replace time.sleep with WebDriverWait for better reliability) time.sleep(2) # Locate the year dropdown (update the selector to match the actual name/id of the year select element on ogimet) year_dropdown = Select(driver.find_element_by_name('year')) # Example: if the select has name="year" # Select the desired year by its visible text year_dropdown.select_by_visible_text('2004')
2. Directly Locate and Click the Option Element
If you prefer not to use the Select class, you can target the specific option element using its text content with an XPATH selector:
time.sleep(2) # Locate the option with the exact text "2004" inside the year dropdown year_option = driver.find_element_by_xpath("//select[@name='year']/option[text()='2004']") # Click the option year_option.click()
Notes for Reliability:
- Replace
[@name='year']with the actual attribute of the year dropdown on ogimet (you can inspect the element to find its name, id, or other unique identifiers). - Instead of relying on
time.sleep(), consider usingWebDriverWaitto wait for the element to be clickable—this makes your script more stable, especially if the page loads slowly: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 year option to be clickable year_option = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//select[@name='year']/option[text()='2004']")) ) year_option.click()
内容的提问来源于stack exchange,提问作者Javier

