使用Python在Selenium中设置<input />下拉列表的值

时间:2015-09-30 13:46:23

标签: python html selenium xpath

我有一个包含带标签的表单的HTML页面。我想使用Selenium设置此标记中下拉列表的值。

这是我检索输入元素的方法:

driver.find_element_by_xpath("/html/body/div[2]/div/div/form/div/div[1]/div[3]/div[1]/div/div[1]/input")

我尝试使用select_month.send_keys("09")设置值,但当我尝试提交表单时网页不接受这个,所以我需要找到另一种方法。

编辑:这是表单的HTML,我确保它是我x路径中的正确元素:

<input autocomplete="off" tabindex="-1" class="ui-select-search ui-select-toggle ng-pristine ng-valid ng-touched" ng-click="$select.toggle($event)" placeholder="Select month" ng-model="$select.search" ng-hide="!$select.searchEnabled || ($select.selected &amp;&amp; !$select.open)" ng-disabled="$select.disabled" type="text">

1 个答案:

答案 0 :(得分:0)

在搞砸了一下并结合了alecxe提出的更好的实践后,这个解决方案起作用了......

driver.find_element_by_xpath("//input[@placeholder='Select month']").click()
driver.find_element_by_xpath("//*[contains(text(), '09')]").click()