0

I have more than one element on page which has same xpath and same id/name , there are two fields on page with same locators i tried to enter value at desired location with below code

element.all(by.xpath('//*[@id="testInstanceScan"]')).get(1).sendKeys('Vkumar');

but I faced error message:

Failed: Index out of bound. Trying to access element at index: 1, but there are only 1 elements that match locator By(xpath, //*[@id="testInstanceScan"] )

if i used element.all('#testInstanceScan').get(1).sendKeys('Vkumar');

i faced error

Failed: Invalid locator Stack: TypeError: Invalid locator

Tried with element.all(by.css('#testInstanceScan')).get(0).sendKeys('Vkumar'); but its not the desired place for this value,. values is entered in previous fields with this 0th intdex.

Ernst Zwingli
  • 1,402
  • 1
  • 8
  • 24
vijay kumar
  • 61
  • 2
  • 12
  • clearly says that `but there are only 1 elements` so the index should be 0 so you have ti try `.get(0).` – Subburaj Nov 29 '17 at 05:42
  • It's incorrect HTML syntax to have two elements with the same ID. If you do that, all the rules break down. You need to fix that problem, and then use something other than ID to identify your two elements. – Sam Hanley Dec 04 '17 at 12:27
  • Possible duplicate of [Several elements with the same ID responding to one CSS ID selector](https://stackoverflow.com/questions/7262195/several-elements-with-the-same-id-responding-to-one-css-id-selector) – Sam Hanley Dec 04 '17 at 12:30

1 Answers1

0

If you need a single element, that has a unique ID, you don't need to use element.all() but element().

Your code should be

element(by.css('#testInstanceScan')).sendKeys('Vkumar');

Or even shorter:

$('#testInstanceScan').sendKeys('Vkumar');

Check also the API on Protractor Homepage

To prove, that there is only one element you can try this:

element.all(by.css('#testInstanceScan')).count().then(function(numberOfElems){
    console.log('For this ID I found '+numberOfElems+' elements.');
})
Ernst Zwingli
  • 1,402
  • 1
  • 8
  • 24
  • Hi Thank you for your reply My page has more than one element with same xpath/css even same id/name. that's why i am using element.all get all elements – vijay kumar Dec 04 '17 at 11:30
  • Your error message disagrees with you. According to your error message "there are only 1 elements that match locator By(xpath, //*[@id="testInstanceScan"] )". So for the example you posted you only have 1 unique match. Therefore `get(0)` would work, but `get(1)` won't, because there is no 2nd element for your locator. – Ernst Zwingli Dec 04 '17 at 11:33
  • Given your update of your question, I assume your locator is wrong. Can you post the HTML of the element you want to get? `
    element
    ` ... and you got invalid locator, because your `element.all()` syntax was wrong... it should be `element.all(by.css('#testInstanceScan'))` or `$$('#testInstanceScan')`
    – Ernst Zwingli Dec 04 '17 at 11:39
  • Hi i corrected my syntax as per your suggestion. As per your suggested code i have found the number of element on page Result is 2 and able to enter values with below mentioned code var size = element.all(by.css('#testInstanceScan')).count(); element.all(by.css('#testInstanceScan')).count().then(function(numberOfElems){ console.log('For testInstanceScan ID I found '+numberOfElems+' elements.'); }) if(expect(size).toBeGreaterThan(1)) element.all(by.css('#testInstanceScan')).get(1).sendKeys('Vkumar'); – vijay kumar Dec 04 '17 at 12:31