【问题标题】:Selenium: Type in an autocomplete input in PythonSelenium:在 Python 中输入自动完成输入
【发布时间】:2018-10-28 09:00:42
【问题描述】:

我正在尝试使用 send_keys 命令输入自动完成功能。它成功地将文本放入输入中,但无法打开自动建议框。当我手动执行此操作时,下拉菜单会打开并显示建议。 这是我的代码:

driver.find_element_by_xpath('//*[@id="geo-location"]/div[1]/input').send_keys('United States')

我尝试在 send_keys 中键入每个字母,我还尝试使用 send_keys('\b') 删除一些字母,但两次尝试都失败了。如何在这种输入中模拟人类打字?

这是我尝试写入的输入源代码:

<div class="geo-location-select  ui-select-container selectize-control single searchable ng-dirty ng-empty ng-touched open" ng-class="{'open': $select.open, 'searchable': $select.searchEnabled, 'not-searchable': !$select.searchEnabled}" id="geo-location" name="geoLocations" ng-model="tad.term" clear-on="UiGeoSelection" on-select="tad.addGeoLocation$($item, $model)" ng-disabled="tad.disabledFn()" disable-default-dropdown="true" style="">
<div class="selectize-input focus" ng-class="{'focus': $select.open, 'disabled': $select.disabled, 'selectize-focus' : $select.focus}" ng-click="$select.open &amp;&amp; !$select.searchEnabled ? $select.toggle($event) : $select.activate()" style="">
<div ng-hide="$select.searchEnabled &amp;&amp; ($select.open || $select.isEmpty())" class="ui-select-match ng-scope ng-hide" ng-transclude="" allow-clear="true" placeholder="Enter location" style="">
<span class="ng-binding ng-scope"></span>
</div>  
<input type="search" autocomplete="off" tabindex="-1" class="ui-select-search ui-select-toggle ng-valid ng-dirty ng-touched ng-empty" ng-click="$select.toggle($event)" ng-class="{'ui-select-search-disabled' : !$select.searchEnabled}" placeholder="Enter location" ng-model="$select.search" ng-hide="($select.selected &amp;&amp; !$select.open)" ng-disabled="$select.disabled" aria-label="Select box" style="width: 440px;"></div><div ng-show="$select.open" class="ui-select-choices ui-select-choices ui-select-dropdown selectize-dropdown single ng-scope" repeat="target in tad.geoLocationsSearchResults" refresh="tad.getGeoLocations$($select.search)" refresh-delay="200" style="opacity: 1;">
<div class="ui-select-choices-content selectize-dropdown-content">
<div class="ui-select-choices-group optgroup" role="listbox">
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label optgroup-header ng-binding ng-hide" ng-bind="$group.name"></div>
<!-- ngRepeat: target in $select.items --></div></div></div>
<ui-select-single></ui-select-single>
<input ng-disabled="$select.disabled" class="ui-select-focusser ui-select-offscreen ng-scope" type="text" id="focusser-2" aria-label="Select box focus" aria-haspopup="true" role="button" disabled="">
</div>

谢谢,

【问题讨论】:

  • 你还需要在html dom中寻找
  • @AshishKamble 我不记得见过使用 SELECT 元素表示自动完成列表的页面。

标签: python-3.x selenium selenium-webdriver input


【解决方案1】:

我必须先用 JS 选择输入,然后填写它和我的文本以打开建议列表。对于那些像我一样苦苦挣扎的人,这是我使用的代码:

而不是

driver.find_element_by_xpath("(//input[@type='search'])[3]").send_keys('Canada')

我用过

element = driver.find_element_by_css_selector("#geo-location .ui-select-search")
driver.execute_script("arguments[0].click();", element)

driver.find_element_by_xpath("(//input[@type='search'])[3]").send_keys('Canada')

PS:我已经选择了另一个元素来选择然后输入那个元素,但它仍然可以正常工作。

【讨论】:

    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    相关资源
    最近更新 更多