我正在尝试通过python/selenium从div下拉列表中选择第二个选项。下面的代码是我到目前为止所掌握的。第一行打开下拉列表并运行。第二行尝试选择2选项并单击它。我试过"2“、"22”和“选项-22”,但似乎都没有效果。
示例Python Selenium代码
#opens drop down
browser.find_element(By.XPATH,".//*[@id='Account']").click()
#selects item from dropdown
browser.find_element(By.XPATH,".//*[@id='Account']/option[22]").click()错误信息:
Message: invalid selector: Unable to locate an element with the xpath expression .//*[@id='Account']/option[22] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[@id='Account']/option[22]' is not a valid XPath expression.示例HTML代码:
<div id="Account" class="">
<select>
<option selected="" value="11">Option-11</option>
<option value="22">Option-22</option>
<option value="33">Option-33</option>
</select>
</div>发布于 2022-06-14 21:40:12
你想要的XPath是:
"//*[@id='Account']//option[@value='22']"
也可以使用以下CSS选择器:
'#Account option[value="22"]'
或者使用SeleniumBase,您可以在一行中执行完整的选择选项:
self.select_option_by_text("#Account select", "Option-22")
(完全披露:我维护SeleniumBase)
https://stackoverflow.com/questions/72623423
复制相似问题