I have several dropdown with the same ID in the xpath (SELENIUM)

0

What should I do if I have 2 dropdown, for example, but the name on the xpath is the same?

How can I do to identify them?

There is no name and the ids are the same. The code for both is this:

<input id="dropdown-input"
       class="form-control ng-pristine 
ng-untouched ng-valid ng-scope ng-empty ng-valid-maxlength"
       translate="" 
       translate-attr-placeholder="form.COUNTRY" 
       ng-model="dropdown.selectedText" 
       ng-click="dropdown.opened = true"
       ng-change="dropdown.opened = true"
       ng-focus="dropdown.opened = true"
       ng-keydown="$event.keyCode == 27 ? dropdown.opened = false : 0"
       ng-class="{'ng-invalid': filteredList.length == 0 && list.length > 0}"
       maxlength=""
       aria-invalid="false"
       placeholder="Country"
       style="background-image: url("data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABHklEQVQ4EaVTO26DQBD1ohQWaS2lg9JybZ+AK7hNwx2oIoVf4UPQ0Lj1FdKktevIpel8AKNUkDcWMxpgSaIEaTVv3sx7uztiTdu2s/98DywOw3Dued4Who/M2aIx5lZV1aEsy0+qiwHELyi+
Ytl0PQ69SxAxkWIA4RMRTdNsKE59juMcuZd6xIAFeZ6fGCdJ8kY4y7KAuTRNGd7jyEBXsdOPE3a0QGPsniOnnYMO67LgSQN9T41F2QGrQRRFCwyzoIF2qyBuKKbcOgPXdVeY9rMWgNsjf9ccYesJhk3f5dYT1HX9gR0LLQR30TnjkUEcx2uIuS4RnI+aj6sJR0AM8AaumPaM/rRehyWhXqbFAA9kh3/8/NvHxAYGAsZ/il8IalkCLBfNVAAAAABJRU5ErkJggg==");
           background-repeat: no-repeat;
           background-attachment: scroll;
           background-size: 16px 18px;
           background-position: 98% 50%;
/>

How can I identify any difference between these dropdowns if they are the same ids?

    
asked by lauraalvarezz1 13.12.2016 в 21:02
source

2 answers

4

The id must be unique. If you use the same id to give the same CSS style to several elements, you should use classes instead.

Here is some more information :

  

The Element.id property represents the identifier of the element,   reflecting the global id attribute.

     

must be a single document, and is often used to   retrieve the item using getElementById . Other common uses of id   include the use of ID elements as a selector when   is styling the document with CSS.

The translation is not too good. In English it says "It must be unique in a document" which means "it must be unique in a document".

Edit: I'll see if with this example I can illustrate you in searches based on other attributes other than id . I will use JavaScript XPath to facilitate the explanation.

function pruebas() {
  console.log("Elementos con id 'dropdown-input': " +
    document.evaluate('count(//*[@id="dropdown-input"])', document, null, XPathResult.ANY_TYPE, null).numberValue);
  console.log("Elementos con placeholder 'Currency': " +
    document.evaluate('count(//*[@placeholder="Currency"])', document, null, XPathResult.ANY_TYPE, null).numberValue);
  var nodos = document.evaluate('//*[@id="dropdown-input"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (var i = 0 ; i < nodos.snapshotLength; i++) {
    console.log("Placeholder del id 'dropdown-input' número " + i + ": " +
      nodos.snapshotItem(i).getAttribute('placeholder'));
  }
}

window.onload = pruebas;
<input id="dropdown-input"
       class="form-control ng-pristine 
ng-untouched ng-valid ng-scope ng-empty ng-valid-maxlength"
       translate="" 
       translate-attr-placeholder="form.COUNTRY" 
       ng-model="dropdown.selectedText" 
       ng-click="dropdown.opened = true"
       ng-change="dropdown.opened = true"
       ng-focus="dropdown.opened = true"
       ng-keydown="$event.keyCode == 27 ? dropdown.opened = false : 0"
       ng-class="{'ng-invalid': filteredList.length == 0 && list.length > 0}"
       maxlength=""
       aria-invalid="false"
       placeholder="Country"
       style="background-image: url('data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABHklEQVQ4EaVTO26DQBD1ohQWaS2lg9JybZ+AK7hNwx2oIoVf4UPQ0Lj1FdKktevIpel8AKNUkDcWMxpgSaIEaTVv3sx7uztiTdu2s/98DywOw3Dued4Who/M2aIx5lZV1aEsy0+qiwHELyi+
Ytl0PQ69SxAxkWIA4RMRTdNsKE59juMcuZd6xIAFeZ6fGCdJ8kY4y7KAuTRNGd7jyEBXsdOPE3a0QGPsniOnnYMO67LgSQN9T41F2QGrQRRFCwyzoIF2qyBuKKbcOgPXdVeY9rMWgNsjf9ccYesJhk3f5dYT1HX9gR0LLQR30TnjkUEcx2uIuS4RnI+aj6sJR0AM8AaumPaM/rRehyWhXqbFAA9kh3/8/NvHxAYGAsZ/il8IalkCLBfNVAAAAABJRU5ErkJggg==');
           background-repeat: no-repeat;
           background-attachment: scroll;
           background-size: 16px 18px;
           background-position: 98% 50%;"
/>
<input id="dropdown-input"
       class="form-control ng-pristine 
ng-untouched ng-valid ng-scope ng-empty ng-valid-maxlength"
       translate="" 
       translate-attr-placeholder="form.CURRENCY" 
       ng-model="dropdown.selectedText" 
       ng-click="dropdown.opened = true"
       ng-change="dropdown.opened = true"
       ng-focus="dropdown.opened = true"
       ng-keydown="$event.keyCode == 27 ? dropdown.opened = false : 0"
       ng-class="{'ng-invalid': filteredList.length == 0 && list.length > 0}"
       maxlength=""
       aria-invalid="false"
       placeholder="Currency"
       style="background-image: url('data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABHklEQVQ4EaVTO26DQBD1ohQWaS2lg9JybZ+AK7hNwx2oIoVf4UPQ0Lj1FdKktevIpel8AKNUkDcWMxpgSaIEaTVv3sx7uztiTdu2s/98DywOw3Dued4Who/M2aIx5lZV1aEsy0+qiwHELyi+
Ytl0PQ69SxAxkWIA4RMRTdNsKE59juMcuZd6xIAFeZ6fGCdJ8kY4y7KAuTRNGd7jyEBXsdOPE3a0QGPsniOnnYMO67LgSQN9T41F2QGrQRRFCwyzoIF2qyBuKKbcOgPXdVeY9rMWgNsjf9ccYesJhk3f5dYT1HX9gR0LLQR30TnjkUEcx2uIuS4RnI+aj6sJR0AM8AaumPaM/rRehyWhXqbFAA9kh3/8/NvHxAYGAsZ/il8IalkCLBfNVAAAAABJRU5ErkJggg==');
           background-repeat: no-repeat;
           background-attachment: scroll;
           background-size: 16px 18px;
           background-position: 98% 50%;"
/>

As you can see, the search XPath //*[@id="dropdown-input"] returns two results, but the search //*[@placeholder="Currency"] only returns one.

I have added a third example in which I search for all the elements with //*[@id="dropdown-input"] and I list the content of their placeholder . As you can see you can do a search and return more than one item as a result. Simply go through the elements to get your data.

    
answered by 13.12.2016 в 21:30
0

Now I do not take the countries, I can say that this is the same for example, Argentina and Australia, this is the only thing that differentiates them in the dropdown:

<li id="dropdown-list" class="ng-binding ng-scope" ng-
if="!dropdown.isButton" ng-repeat="item in dropdown.list | 
filter:dropdown.selectedText as filteredList" ng-    
click="dropdown.clicked(item)" ng-keydown="$event.keyCode == 27 ? 
dropdown.opened = false : 0" role="button" tabindex="0">
AUSTRALIA 

<li id="dropdown-list" class="ng-binding ng-scope" ng-
if="!dropdown.isButton" ng-repeat="item in dropdown.list | 
filter:dropdown.selectedText as filteredList" ng-
click="dropdown.clicked(item)" ng-keydown="$event.keyCode == 27 ? 
dropdown.opened = false : 0" role="button" tabindex="0" style="">
ARGENTINA 
    
answered by 14.12.2016 в 16:50