我想使用Selenium和Python从选择字段中选择一个选项。
HTML如下:
<select autocomplete="off" class="style_input_item" name="AccountEnable" id="Enable" value="0" onchange="onPageDataChange()">
<option value="0" selected="selected"><script>T("Disabled")</script>Disabled</option>
<option value="1"><script>T("Enabled")</script>Enabled</option>
</select>
我尝试如下:
driver.find_element_by_xpath('//*[@id="Enable"]/option[value="1"]').click()
我收到了这个错误:
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“// * [@ id =”Enable“] / option [value =” 0 “]”}
试一试:
mydriver.find_element_by_xpath('//*[@id="Enable"]/option[@value="1"]').click()
要么
mydriver.find_element_by_xpath('//*[@id="Enable"]/option[2]').click()
确保包括选择:
from selenium.webdriver.support.select import Select
然后
select = Select(driver.find_element_by_id('Enable'))
select.select_by_index(0)