javascript - 如何使用 Python/Selenium webdriver 处理 Angularjs/Javascript 下拉菜单?

标签 javascript python angularjs selenium xpath

我想在 Chromium 浏览器上使用 Python 和 Selenium webdriver 自动执行一些浏览器任务。我的 Python 脚本已经能够登录、导航到子页面/进行一些点击,以及在表单中插入内容。

我的问题是一个强制性的下拉列表,我必须在其中选择一些东西才能继续。我认为该网页当时包含 angularjs/javascript(下面代码中的第三行)来创建下拉列表,我不知道如何处理。

问题似乎是 1) 定位元素(xpath 有时似乎会改变),以及 2) 我无法点击或发送我找到的键。我还尝试了一些“WebDriverWait”和 sleep 命令以及“wait.until(expected_conditions.visibility_of_element_located((By.XPATH, ...))”......到目前为止没有运气。

仅使用 Python 和 Selenium 是否有可能解决该问题? 还是我需要像 Protractor 这样的东西(而且 Protractor 只适用于 Javascript 命令)?我还看过 Pytractor...

我在这方面是个新手,有人可以解释一下解决这个问题的好方法吗?提前致谢...:)

网页代码如下所示(使用 Firebug/Firepath 抓取):

<div class="ng-scope ng-isolate-scope" model-contains-key="true" ref="salutations" cat-input-select="editDetail.salutation">
    <div id="s2id_autogen1" class="select2-container form-control ng-untouched ng-valid ng-dirty ng-valid-parse">
        <a class="select2-choice select2-default" tabindex="-1" href="javascript:void(0)">
            <span id="select2-chosen-2" class="select2-chosen"/>
            <abbr class="select2-search-choice-close"/>
            <span class="select2-arrow" role="presentation">
                <b role="presentation"/>
            </span>
        </a>
        <label class="select2-offscreen" for="s2id_autogen2"/>
        <input id="s2id_autogen2" class="select2-focusser select2-offscreen" type="text" role="button" aria-haspopup="true" aria-labelledby="select2-chosen-2"/>
        <div class="select2-drop select2-display-none select2-with-searchbox">
            <div class="select2-search">
                <label class="select2-offscreen" for="s2id_autogen2_search"/>
                <input id="s2id_autogen2_search" class="select2-input" type="text" aria-autocomplete="list" aria-expanded="true" role="combobox" spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off" aria-owns="select2-results-2" placeholder=""/>
            </div>
            <ul id="select2-results-2" class="select2-results" role="listbox"/>
        </div>
    </div>
    <select class="form-control ng-untouched ng-valid ng-dirty ng-valid-parse" ng-change="modelChanged(); changeCallback({value: selectValue.value})" ng-readonly="readonly" ng-disabled="disabled" ng-model="selectValue.value" ui-select2="{dropdownAutoWidth: 'true', allowClear: 'false'}" tabindex="-1" title="" style="display: none;">
        <option value=""/>
        <!-- ngRepeat: option in options -->
        <option class="ng-binding ng-scope ng-isolate-scope" cat-i18n="xxxxxxx.salutation.Mr" value="Mr" ng-repeat="option in options">Mr</option>
        <!-- end ngRepeat: option in options -->
        <option class="ng-binding ng-scope ng-isolate-scope" cat-i18n="xxxxxxx.salutation.Ms" value="Ms" ng-repeat="option in options">Ms</option>
        <!-- end ngRepeat: option in options -->
    </select>
    <!-- ngIf: infoText -->
</div>

最佳答案

为了自动化使用 Angular JS 开发的任何网页,我们必须使用开源工具“Protractor”。 Selenium 本身不会支持 Angular JS Web 应用程序自动化。有关 Protractor 的更多信息,请参阅以下文章

Testing Angular JS application using Protractor

Protractor 也使用与 selenium 相同的命令。

希望这对您有所帮助。

关于javascript - 如何使用 Python/Selenium webdriver 处理 Angularjs/Javascript 下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37000714/

相关文章:

javascript - Videojs Player HD 切换更改

python - 理解 OptionParser

python - MaxRetryError : HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError ('Connection aborted.' , 错误 (111, 'Connection refused' )))

angularjs - 使用 Angular $resource 的 POST 请求

javascript - 使用ngmodel选择标签不显示 Controller 中设置的默认值

javascript - 谷歌地图集群下拉菜单?

javascript - 从 javascript ruby​​ on Rails 调用 Controller 方法

javascript - 我正在尝试在 javascript 中验证没有警报功能的文本字段,但它不起作用

python - 类型错误 : __init__() got an unexpected keyword argument 'parent'

angularjs - 多个 $http.get 请求响应被交换。有解决办法吗?