如何使用Selenium和Python从下拉列表中选择值

问题描述 投票:0回答:2

我想使用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 “]”}

python-3.x selenium select xpath
2个回答
0
投票

试一试:

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[@value="1"]').click()

要么

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[2]').click()

0
投票

确保包括选择:

from selenium.webdriver.support.select import Select

然后

select = Select(driver.find_element_by_id('Enable'))
select.select_by_index(0)
© www.soinside.com 2019 - 2024. All rights reserved.