无法在Python中使用Selenium选择下拉菜单

2024-10-16 11:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法在这个网页中为Selenium(python)的自动化选择一个特定的元素。在

这两个字段,取自网页,是我要找的:

enter image description here

当我右键单击并单击“inspect element”时,我发现了以下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> &nbsp;
<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>

“Client ID”字段既是一个下拉字段,也是一个文本字段(由用户选择所述用户是想键入客户机ID还是从下拉菜单中选择它)。 代理ID只是一个文本字段,用于输入数字。在

在硒元素中我似乎两个都选不出来。例如,为了选择客户机ID,我尝试了以下Python命令(单独):

^{pr2}$

可悲的是,似乎什么都没用,我也不知道为什么。我看到的唯一的另一种可能性是选择标签,即<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之后,它还列出了许多我不想问的行话。难道我没有充分利用口译员的调试响应吗?在


Tags: divclientiddbvaluemodeelementng
1条回答
网友
1楼 · 发布于 2024-10-16 11:27:27

The only other possibility I see is selecting the labels, i.e. Client ID: and then telling the interpreter to go to the next element and select that- though I'm not sure what the correct syntax would be.

这可以通过^{} axis来实现:

client_id_box = browser.find_element_by_xpath("//label[. = 'Client ID:']/following-sibling::db-client-combobox")

然后,一旦有了client id box元素,就可以找到内部输入:

^{pr2}$

对于下拉列表,您可能首先需要打开它,然后选择所需的选项:

client_id_box.find_element_by_id("combobox-list").click()  # open the dropdown
# TODO: select option

请注意,为了使事情更可靠并避免计时错误,您可能需要开始使用Explicit Waits。在

相关问题 更多 >