【发布时间】:2026-02-16 04:40:01
【问题描述】:
我有以下下拉列表,我想从中选择值“上一年”:
(我已经手动选择了值)
问题是双重的,我很难用我的代码“找到”或找到这个下拉列表,并且“id”中的数字会逐页更改,所以我无法找到它通过它的“id”。我正在尝试使用以下 Python 3 代码:
select = driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]')
select.click()
# then click the "Previous Year" text
我也试过了:
select = Select(driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]'))
select.select_by_value("Previous Year")
现在这个站点是一个私人工作站点,所以我不能授予访问权限,但我希望有人能指出我还能尝试找到这个元素并选择正确的下拉值?我试过用“css”和“xpath”来定位这个字段,没有运气。
xpath = //*[@id="dropdown-intervaltype31442"]
css_selector = #dropdown-intervaltype31442
full_xpath = /html/body/div[4]/div[2]/div/div[3]/div[3]/div/div[2]/div/form/table/tbody/tr/td[2]/div/div[1]/select
任何要测试的想法都会有所帮助。谢谢!
【问题讨论】:
标签: python-3.x selenium xpath drop-down-menu css-selectors