0

For the mentioned code

<div class="md-input-infix">
  <!--template bindings={}-->
  <input class="md-input-element ng-pristine ng-valid ng-touched" id="md-input-19-input" spellcheck="false" type="text">
    <!--template bindings={}--><!--template bindings={}-->
    <label class="md-input-placeholder md-empty md-float" for="md-input-19-input">First name * 
    <!--template bindings={}-->
  </label>
</div>

1.)In this code, id changes every time.So,I will not be able to select id as locator

2.)I can use CSS by type="text",but on the page there is many more elements with this type ="text".So, this will also not work.

3)I am working on chrome for inspecting element.In this case, if I could select XPath, it will be coming with id inside it.

4)I don't want to go with the absolute XPath.

6)Someone can provide me the solution for this problem.

Anjali Singh
  • 19
  • 1
  • 6

3 Answers3

1

Try this one:

var elmDiv = element.all(by.cssContainingText('div', 'First name')).last();
var elmInput = elmDiv.element(by.css('[id^="md"]'))
Paul Co
  • 447
  • 2
  • 9
  • ,hey thanks a lot ,it is working very well with all my fields.So, I just want to know the logic behind this .Using elmInput.click(),I am able to click on the elements and now it is not showing me the error as "More than one element exist with this locator". Can u pls tell me the logic as how u r using last .Because as per my knowledge is concerned about last ,it is going to retrieve last element. – Anjali Singh May 04 '17 at 03:56
  • hi @AnjaliSingh , what happens is we tried to get all `div` that contains the text `First name` and based from your html, the last `div` contains the `input` element. Since we now know that the `input` element is inside the `div`. We are able to locate it easily. :) – Paul Co May 04 '17 at 07:12
0

How about using a CSS selector?

 input[class="md-input-element ng-pristine ng-valid ng-touched"]

Or you could try locating the parent element first with:

div[class="md-input-infix"]

And then search within its descendants.

Try and see if this helps:

element.all(by.css('input[class="md-input-element ng-pristine ng-valid ng-touched"]')).each(function(element) {
  element.sendKeys('blah blah');
});
5tr1k3r
  • 1
  • 2
  • Tried these both ways. I can use this way for the first field that is " first name",but there is the same class for last name field too. So, It will show me the error saying more than one element found. – Anjali Singh May 03 '17 at 11:04
  • Try this: http://stackoverflow.com/a/28464809/5787172 . Or you could loop through all the elements if you need each one. – 5tr1k3r May 03 '17 at 11:42
  • Then for the second field what I will do. – Anjali Singh May 03 '17 at 12:12
  • @AnjaliSingh have you tried using the code i added in the edit? – 5tr1k3r May 04 '17 at 07:23
  • Hey, this will work only for the first field but not for the other fields.I have mentioned in my query that there are many elements in the page with this class name . – Anjali Singh May 04 '17 at 09:03
  • It's a loop, it's supposed to work for every element with this specific selector. – 5tr1k3r May 04 '17 at 13:28
0

Try below code:

var el = element(by.css("label:contains('Low Touch Client Name')"));
var elementID = el.getAttribute('id');    
var inputBox = element(by.id(elementID));
inputBox .sendKeys('my text');
peter pawar
  • 106
  • 1
  • 9