我无法在此网页中选择特定元素以便在Selenium(python)中实现自动化。
这两个字段来自网页,是我正在寻找的:
当我右键单击并单击"检查元素时,"我找到了以下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>
&#34;客户ID&#34;字段既是下拉字段又是文本字段(由用户决定是否所述用户想要键入客户端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>
,然后告诉解释器转到下一个元素并选择它 - 尽管我不确定正确的语法是什么。
有人可以帮忙吗?
更新:遵循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后,它还列出了许多其他的行话,我没有&#39;我的意思是要求。我没有充分利用解释器的调试响应吗?
答案 0 :(得分:1)
我看到的唯一另一种可能性是选择标签,即客户端ID:然后告诉解释器转到下一个元素并选择它 - 尽管我不确定正确的语法是什么。
这可以通过following-sibling
axis:
client_id_box = browser.find_element_by_xpath("//label[. = 'Client ID:']/following-sibling::db-client-combobox")
然后,一旦你有了客户端ID框元素,就可以找到内部输入:
client_id_input = client_id_box.find_element_by_tag_name("input")
client_id_input.click() # TODO: you might not need it, please check
client_id_input.send_keys("Test")
对于下拉列表,您首先可能需要打开它,然后选择所需的选项:
client_id_box.find_element_by_id("combobox-list").click() # open the dropdown
# TODO: select option
请注意,为了使事情更可靠并避免计时错误,您可能需要开始使用Explicit Waits。