简体   繁体   English

如何通过硒获得ExtJS组合框的选定值?

[英]How to get the selected value of ExtJS combobox through selenium?

I am having a webpage rendered using ExtJS. 我正在使用ExtJS呈现网页。 It renders multiple number of Comboboxes with some generated ID. 它使用一些生成的ID呈现多个Combobox。 And each combobox have a different option selected. 每个组合框都有不同的选项。 How can I find out which value is selected in each combo? 如何找出每个组合中选择的值?

While debugging the the HTML DOM I observed that the ExtJS rendering DIV is different and the selectable options are rendered in different DIV at the end. 在调试HTML DOM时,我观察到ExtJS渲染DIV是不同的,并且可选择的选项最后在不同的DIV中呈现。 So I am not able to define any XPath to find out the selected value. 所以我无法定义任何XPath来找出所选的值。

Following is the code snippet 以下是代码段

<div class="guide-bg" id="sample"><table>
    <tr><td id="row1"> </td></tr>
    <tr><td id="row2"></td></tr>
</table></div>

<script>
Ext.namespace('Ext.exampledata');
Ext.exampledata.states = [['Alabama'],[ 'Alaska'],['Arizona']];

var store1 = new Ext.data.SimpleStore({
    fields: [ 'state'],
    data : Ext.exampledata.states
});

var combo1 = new Ext.form.ComboBox({
        store: store1, displayField:'state', typeAhead: true,
        mode: 'local', forceSelection: true, triggerAction: 'all',
        emptyText:'Select...', selectOnFocus:true, renderTo: 'row1'
});

var combo2 = new Ext.form.ComboBox({
        store: store1, displayField:'state', typeAhead: true,
        mode: 'local', forceSelection: true, triggerAction: 'all',
        emptyText:'Select...', selectOnFocus:true, renderTo: 'row2'
});
</script>

After rendering the HTML generated at the 'sample' div is 渲染在'sample'div生成的HTML后

<div class="guide-bg" id="sample">
  <table>
    <tbody><tr><td id="row1"> 
    <div class="x-form-field-wrap x-form-field-trigger-wrap" id="ext-gen148" style="width: 206px;">
        <input type="text" name="ext-comp-1028" id="ext-comp-1028" autocomplete="off" size="24" class="x-form-text x-form-field x-form-empty-field" style="width: 181px;">
        <img class="x-form-trigger x-form-arrow-trigger" src="sp.gif" id="ext-gen149">
    </div></td></tr>
    <tr><td id="row2">
    <div class="x-form-field-wrap x-form-field-trigger-wrap" id="ext-gen150" style="width: 206px;">
        <input type="text" name="ext-comp-1029" id="ext-comp-1029" autocomplete="off" size="24" class="x-form-text x-form-field x-form-empty-field" style="width: 181px;">
        <img class="x-form-trigger x-form-arrow-trigger" src="sp.gif" id="ext-gen151">
    </div></td></tr>
  </tbody></table>
</div>  

And the options are present independently at the end of the DOM like this 这些选项在DOM的末尾独立存在

<div class="x-layer x-combo-list " id="ext-gen146" style="position: absolute; z-index: 12005; visibility: hidden; 
    left: -10000px; top: -10000px; width: 204px; height: 129px; font-size: 12px;">
    <div class="x-combo-list-inner" id="ext-gen147" style="width: 204px; height: 129px;">
        <div class="x-combo-list-item x-combo-selected">Alabama</div>
        <div class="x-combo-list-item">Alaska</div>
        <div class="x-combo-list-item">Arizona</div>
    </div>
</div>

I cannot just iterate over each x-combo-list-inner as there is no mapping between the div that renders the combo and the div that holds the available options. 我不能只迭代每个x-combo-list-inner ,因为渲染组合的div和保存可用选项的div之间没有映射。 If it was just a single combo then it would have been fine, but in my case I have multiple combo boxes with same values. 如果它只是一个组合,那么它会没问题,但在我的情况下,我有多个具有相同值的组合框。

Eexecute JS using selenium( How to use JavaScript with Selenium WebDriver Java ). 使用selenium Eexecute JS( 如何在Selenium WebDriver Java中使用JavaScript )。 In js you can use "Ext.ComponentQuery.query('combo')[0].value" code to get combo value 在js中你可以使用“Ext.ComponentQuery.query('combo')[0] .value”代码来获取组合值

To get text in dropDown element which is selected you need to iterate through all divs in x-combo-list-inner, gets it class value and if it contains x-combo-selected then return it's text. 要获取所选的dropDown元素中的文本,您需要迭代x-combo-list-inner中的所有div,获取它的类值,如果它包含x-combo-selected,则返回它的文本。 Not sure about correctness of js but this should look like (i haven't tested this code): 不确定js的正确性,但这应该看起来像(我还没有测试过这段代码):

driver.findElement(webdriver.By.css(".x-combo-list-inner")).then(function(parentElement) {
    return parentElement.findElements(".x-combo-list-item").then(function(innerElements) {
        return innerElements.forEach(function(elem) {
            return elem.getAttribute('class').then(function(classValue) {
                if(classValue.indexOf('x-combo-selected') > -1) {
                    return elem.getText();
                }
            });
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM