我無法在此網頁中選擇Selenium(python)中的自動化特定元素。無法在Python中使用Selenium選擇下拉菜單
這兩個字段,從網頁上當,目前我正在尋找的東西:
當我右鍵點擊,點擊「檢查元素」,我發現下面的HTML標記(好了,它的一個子集):
<label>Client ID:</label>
<db-client-combobox default-value="vm.clientId" on-select="vm.onClientSelected(clientId)" class="ng-isolate-scope">
<db-combobox placeholder="Select a client" list="clientNames" default-value="defaultValue" on-select="onClientSelected(value)" mode="longlist" class="ng-isolate-scope">
<div class="input-group combobox dropdown">
<input type="text" class="form-control ng-pristine ng-valid ng-touched" placeholder="Select a client" data-toggle="dropdown" ng-model="itemSelected" ng-change="onInputKeyUp()" ng-focus="onInputFocus()" aria-expanded="false">
<span class="input-group-addon dropdown-toggle btn" id="combobox-list" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="caret"></span>
</span>
<ul class="dropdown-menu" aria-labelledby="combobox-list">
<!-- ngIf: mode == 'longlist' -->
<div class="sf-viewport sf-viewport-biglist real ng-scope" ng-if="mode == 'longlist'" style="overflow: auto; max-height: 300px;">
<div style="margin: 0px; padding: 0px; border: 0px; box-sizing: border-box; height: 67912px;">
</div>
</div>
<!-- end ngIf: mode == 'longlist' -->
<!-- ngIf: mode == 'shortlist' -->
</ul>
</div>
</db-combobox>
</db-client-combobox>
</div>
<div class="form-group">
<label>Agent ID:</label>
<input type="text" class="form-control js-call-type ng-pristine ng-valid ng-touched" ng-model="vm.agentId">
</div>
「客戶端ID」字段既是一個下拉列表,文本字段(它是由用戶選擇是否說用戶要輸入客戶ID或選擇它從一個下拉菜單中)。 代理商ID只是一個輸入數字的文本字段。
我似乎無法在Selenium中選擇。選擇客戶端ID,例如,我已經試過以下Python命令(單獨):
client_id_field = browser.find_elements_by_css_selector('select')
client_id_field = browser.find_element_by_css_selector("input.form-control ng-pristine ng-valid ng-touched")
client_id_field = browser.find_element_by_css_selector("input.form-control.ng-pristine.ng-valid.ng-touched")
client_id_field = browser.select_by_xpath('/html/body/div[1]/div/div[1]/div/div/div[1]/db-client-combobox/db-combobox/div/input')
client_id_field = browser.select_by_class_name('form-control ng-pristine ng-valid ng-touched')
可悲的是,似乎沒有任何工作,我不知道爲什麼。我看到的唯一的另一種可能性是選擇標籤,即<label>Client ID:</label>
,然後告訴解釋器轉到下一個元素並選擇 - 儘管我不確定正確的語法是什麼。
任何人都可以幫忙嗎?
UPDATE:以下alecxe的建議後,試圖將
client_id_box = browser.find_element_by_xpath("//label[. = 'Client ID:']/following-sibling::db-client-combobox")
我得到了在命令行提示符下輸入下面的錯誤:
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Una
ble to find element with xpath '//label[. = 'Client ID:']/following-sibling::db-
client-combobox'","request":{"headers":{"Accept":"application/json","Accept-Enco
ding":"identity","Connection":"close","Content-Length":"147","Content-Type":"app
lication/json;charset=UTF-8","Host":"127.0.0.1:53214","User-Agent":"Python-urlli
b/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"ses
sionId\": \"d67f3550-e736-11e6-a1df-2f9c010cd01f\", \"value\": \"//label[. = 'Cl
ient ID:']/following-sibling::db-client-combobox\"}","url":"/element","urlParsed
":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","re
lative":"/element","port":"","host":"","password":"","user":"","userInfo":"","au
thority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]
},"urlOriginal":"/session/d67f3550-e736-11e6-a1df-2f9c010cd01f/element"}}
Screenshot: available via screen
東西讓我覺得奇怪的(對不起,如果這聽起來很幼稚)是命令行表示unable to find element with xpath...
,然後在列出我要求的xpath之後,它還列出了許多我不想要求的術語。我沒有充分利用解釋器的調試響應嗎?
謝謝你的迅速回應,alecxe。我試着用你的建議來使用'follow-sibling'軸,但我仍然有一個奇怪的錯誤。我更新了我的問題以包含錯誤響應。你介意檢查一下嗎?不用說,我對網絡抓取仍然很陌生。 – daOnlyBG
@daOnlyBG當然,如果你使用'WebDriverWait'和'presence_of_element_located'預期條件呢? (可能是時間錯誤) – alecxe
當然可以。我會給它一個鏡頭。 – daOnlyBG