2016-05-03 142 views
4

我想在Chromium瀏覽器上使用Python和Selenium webdriver自動執行一些瀏覽器任務。我的python腳本已經能夠登錄,導航到一個子頁面/做一些點擊,並將一些東西插入到表單中。如何使用Python/Selenium webdriver處理Angularjs/Javascript下拉菜單?

我的問題是一個強制性的下拉列表,我必須選擇一些東西,然後才能繼續。我認爲該網頁包含angularjs/JavaScript在這一點(下面的代碼中的第三行)創建下拉,我不知道如何處理。

問題似乎是1)找到元素(XPath似乎有時會改變),和2)我無法點擊或發送鍵我發現。我也嘗試了一些「WebDriverWait」和睡眠命令和「wait.until(expected_conditions.visibility_of_element_located((By.XPATH,...))」...迄今爲止沒有運氣。

它是在所有可能的解決這個問題,只有Python和Selenium? 或者我需要像量角器(和量角器只是與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> 

回答

0

爲了自動化在Angular JS中開發的任何網頁,我們必須使用開源工具「量角器」。 Selenium本身不會支持Angular JS Web應用程序自動化。有關量角器的更多信息,請參閱下面的文章

Testing Angular JS application using Protractor

量角器還使用了硒使用相同的命令。

希望這會有所幫助。

相關問題