![](/img/trans.png)
[英]How to handle Javascript click by using selenium Webdriver?
[英]How to handle Angularjs / Javascript dropdown using Python / Selenium webdriver?
我想在Chromium瀏覽器上使用Python和Selenium webdriver自動執行一些瀏覽器任務。 我的python腳本已經能夠登錄,導航到子頁面/做一些點擊,並在表單中插入一些東西。
我的問題是一個強制下拉列表,我必須先選擇一些東西才能繼續。 我認為該網頁包含angularjs / javascript(下面的代碼中的第三行)來創建下拉列表,我不知道如何處理它。
問題似乎是1)找到元素(xpath似乎有時會改變),2)我無法點擊或發送密鑰到我發現的。 我也嘗試了一些“WebDriverWait”和睡眠命令以及“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應用程序自動化。 有關量角器的更多信息,請參閱以下文章
量角器也使用selenium使用的相同命令。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.