3

I am trying to get the Xpath of a label text in a web page that I am trying to automate. The application is angular based, so there are a lot of angular related stuff on the page.

However, I am trying to automate this using Selenium and Python. The element in question is the error message highlighted in the image - enter image description here

The html pertaining to the error message is highlighted in the section below. What would be suitable locator for this element?

I have tried a lot of combinations- using Xpaths and CSS, but I am coming up empty handed. The absolute xpath - which is

html/body/app-root/ng-component/div/div/div[2]/form/div[2]/div/label is the only one that works for this case, but I don't want to use it.

Some of the combinations that I have tried - using Xpath's

//form[@class='ng-untouched']/div[2]/div/label

//div[@class='login-inner']/div/label

both of these don't work and Selenium throws a NoSuchElement exception.

Using CSS Selector, I used this -

element.find_element_by_css_selector(".login-inner.ui-g-12 div label")

Any kind of help would be appreciated.

1
  • //label[text()='password required'] Jan 20, 2017 at 7:14

2 Answers 2

5

The reason the first two don't work is because you didn't include the full class value for either the form or div element in your xpath.

Try

//form[@class='ng-untouched ng-pristine ng-valid']/div[2]/div/label

OR

//form[contains(@class,'ng-untouched')]/div[2]/div/label

Note the use of contains() to match against a partial value. I've found this to be very useful when working against angular.

But why not simply get the label itself using the text property?

//label[text()='password required']
4
  • 1
    Well the first and third xpath don't work- and I have no idea why. But the second one, using the contains function work. Thanks for this btw. Jan 18, 2017 at 7:10
  • I'm a little surprised the first and third didn't work. Are you using a WebDriverWait to ensure the find isn't performed before the element exists in the DOM? Jan 18, 2017 at 16:06
  • Yeah, I am adding sufficient amount of waiting time for the participant to be located. In both of the failed elements, the error was element with this locater could not be found. Although I can check this using the IDE (as mentioned in another answer) to verify if the xpath is indeed correct. Jan 20, 2017 at 8:13
  • A trick I use quite often (as I don't like using the IDE) is to use Chrome dev tools (F12). In the console, you can use $x("<your xpath here>"). The result will be an array of all elements that match that xpath. Jan 20, 2017 at 15:39
0

Use selenium IDE. Record the same test steps and get the highlighted message. Right click and make a assertion on that text. Go to the code and check how selenium IDE handles it. Use the same on your code.

1
  • While this is certainly an option, in my brief experience with the IDE, it often spits out fragile xpaths. Example: //div[2]/table/tr[5]/td[3]/span. The problem with something like this is that if a decision is made to rearrange the UI, all tests requiring that xpath locator will break. (They may have improved the IDE since last I used it). Mar 28, 2017 at 20:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.