-1

How do I find both Data QA Id and CSS Class in Selenium C# ?

Trying to locate both DataQAid attribute and also see if its class ng-invalid, (Angular Material Textbox Error)

This is what wrote so far, Open to using CssSelector or anything else, instead of Xpath

private By PhoneError => By.XPath("//*[@attr.data-qa='homephone']");

enter image description here

Resource: Find element in Selenium using XPATH or CSS Selector

3
  • First find class object. Then search in class object for the data-qa. Commented Aug 12, 2020 at 19:59
  • hi @jdweng feel free to write in answer, and I can send points, thanks Commented Aug 12, 2020 at 19:59
  • hi @jdweng someone placed answer below, I am going to try that, hopefully it works too, thanks Commented Aug 12, 2020 at 20:30

3 Answers 3

1

A working XPath could be :

//div[starts-with(@class,'mat-form-field')]/input[contains(@data-qa,'homephone-update') and contains(@class,'ng-invalid')]

Output : 1 node.

  <input _ngcontent-ng-cli-universal-cli matinput formcontrolname="homephoneNumber" placeholder="'HomePhone'" class="mat-input-element mat-form-field-autofill-control ng-tns-c64-42 ng-touched cdk-text-field-autofil)-monitored ng-dirty ng-invalid" maxlength="50" id="mat-input-26" data-placeholder="'Home Phone'" aria-invalid="true" aria-required="false" data-qa=" homephone-update">
Sign up to request clarification or add additional context in comments.

Comments

0

I prefer using CSS selectors because of the better support and speed over XPath. I've tested the below with the HTML you provided and it works.

private By PhoneError => By.CssSelector("input[ng-invalid][data-qa='homephone-update']");

2 Comments

so it By.CssSelector(input[ng-invalid][data-qa='homephone-update']), can you write in final answer? and I can send points, thanks
@Artportraitdesign1 Updated answer that parallels your posted code.
0

You can use and and contains=>

private By PhoneError => By.XPath("//*[@attr.data-qa='homephone' and contains(@class, ‘ng-invalid’)]");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.