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

标签 python html selenium selenium-webdriver web-scraping

我无法在此网页中选择特定元素以在 Selenium (python) 中进行自动化。

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

enter image description here

当我右键单击并单击“检查元素”时,我发现了以下 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>

“客户 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>然后告诉解释器转到下一个元素并选择它 - 虽然我不确定正确的语法是什么。

有人能帮忙吗?

更新:按照 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 之后,它还列出了很多我不想要求的其他行话。我没有充分利用解释器的调试响应吗?

最佳答案

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.

这可以通过 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 .

关于python - 无法在 Python 中使用 Selenium 选择下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945993/

相关文章:

javascript - 统计一个div有多少个元素

html - 让 <div> 元素填充另一个 <div> 元素

html - CSS/HTML : Dynamic `hat` border on hover of menu item

python - numpy 数组和矩阵有什么区别?我应该使用哪一个?

python:使用元素树处理 XML 项目数组,最好是 "merge"

Selenium 未与 Firefox 12.0 一起运行

node.js - 如何将组合键发送到 selenium chromedriver?

python - 使用 selenium python 截取整个页面截图

iphone - 通过 Twisted 服务器进行 Apple 商店收据验证

Python Flask - 如何通过 cookie/session 记住匿名用户?