【问题标题】:Setting the value of an <input> dropdown in Selenium with Python使用 Python 在 Selenium 中设置 <input> 下拉列表的值
【发布时间】:2015-09-30 13:46:23
【问题描述】:

我有一个 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-path 中的正确元素:

<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">

【问题讨论】:

  • 同级还有其他输入元素吗?您可以尝试使用 firebug 来确保元素位置正确吗?
  • 我已确保 Xpath 正确。
  • driver.find_element_by_xpath("//input[@placeholder='Select month']") 怎么样?
  • 好的,这也适用于定位元素,但不能解决我的下拉问题。
  • 下拉菜单?它在哪里?

标签: python html selenium xpath


【解决方案1】:

经过一番折腾并结合了 alecxe 提出的更好实践后,这个解决方案奏效了……

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多