I recorded a script using the (Windows) Dynatrace Recorder. For one of the CSS locators for the Click action for one step the recorder chose this:
(The "Abc Xyz" text is actually something else, but you get the idea.)
After studying this a while, I think I figured out that this is saying, "Look for a DIV tag that is 7 levels deep that contains 'Abc Xyz'".
There are several other DIV tags that are at this same level, but only one that contains the text "Abc Xyz", and so this works.
That's all well and good, but what if the developers of the site I'm writing this script for restructure the page such that this particular DIV tag is no longer 7 levels deep? I'd like to be able to give a less specific version by removing the ":eq(6)" portion of this specification, but this doesn't work.
How can I "future proof" this specification while keeping it specific enough such that it's looking for "Abc Xyz" in a DIV tag, but where it just might not be at the same level where it exists now? I know I could use an ID attribute if this DIV tag had one, but it doesn't. The DIV tag in question is part of an Angular mat-button-toggle attribute. Here's more of the HTML that shows this DIV tag in more context:
<mat-button-toggle _ngcontent-c28="" class="mat-button-toggle" id="mat-button-toggle-2"> <label class="mat-button-toggle-label" for="mat-button-toggle-2-input"> <input class="mat-button-toggle-input cdk-visually-hidden" type="radio" id="mat-button-toggle-2-input" name="mat-button-toggle-group-0" aria-label=""> <div class="mat-button-toggle-label-content"> <div _ngcontent-c28="" translate=""> Abc Xyz </div> </div> </label> <div class="mat-button-toggle-focus-overlay"> </div> </mat-button-toggle>
Answer by mark e. ·
You've got 3 ways to uniquely identify a control - CSS locator, DOM locator and XPath (we record only the first 2, you can manually add XPath). We record multiple locators if one is not sufficiently unique. On playback we will attempt to find each locator in turn until one is found.
Given this you may want to open your app in the browser and inspect the element and see what selector the browser returns to you - may be slightly different than Gomez but can be included. Or using Chrome (for instance) you can get the xpath of the element that gives you additional control over how to locate (e.g. the xpath for the HTML content in your post above is "//*[@id="question-202945"]/div/pre"), even using wildcards in your xpath expression.
Answer by Ted H. ·
OK, I played with this some more and I got the following XPath specification to work correctly as locators for both Validate and Click actions:
This seems satisfactorily generic enough to be somewhat "future proof", yet specific enough to reliably find the desired DIV tag. (Whew.)
I'm still not sure why this works for Validate but not for Click:
But this version might be a little too generic anyway in that it doesn't narrow the scope down to DIV tags, so I'm happy with the version that I finally got working.
Answer by Ted H. ·
I'm still struggling with this. Here's the XPath that Chrome's Developer Tools gives for the "Abc Xyz" text in the HTML snippet in my original post:
This indeed works as a Click action locator, and is probably superior in some ways to the CSS locator from my original post. However, it's still more specific than I would desire. The "mat-button-toggle-2" id value is likely going to change to "mat-button-toggle-5" in a future release of the code, so this locator will fail at that point.
I tried something as generic as this:
//*[. = 'Abc Xyz']
I can tell that the Click action isn't objecting to this because I don't get an XPath error for it like I do for some other XPath values that are simply not valid. However, the actual click action isn't taking place, so this locator does not work in that respect. Interestingly, this locator does work in a Validate action for a previous step. I can tell that it's working because I see the "Abc Xyz" text getting a yellow highlighted border during playback.
Any ideas why this more generic locator doesn't work for a Click action, or any other ideas I might try?
When i am trying to record the steps in the recorder page are not visible but once i stop the recording and play the recorded steps then i am able to see the page.what is the reason? why iam not able to see the pages while recording? 4 Answers