guht February 2016

Find node from known starting point in totally dynamic webpage using c# and XPath?

I am trying to write automated smoketest for an internal website. The problem is the website is almost primarily dynamically generated. So any kind of unique identifier like ID is a combination of known string prefix and ends with a radomized number.

I also can NOT depend on the order, so using things like div[2] or span[25] will not be reliable, UNLESS there is some way to grab the count of the span/div/input based on where I am currently located in the DOM by the KNOWN TEXT VALUE.

For example I can find the known text value. If I can somehow programatically determine that the span for this known text value is 55 , and I know that it is nested 2 deep from the other element I am looking for, then I could do something like "//span[55 - 2]/input".

The best I can do is navigate the DOM to some KNOWN text value, and work up OR down from there.

Given that, in the example below, how would I navigate to the INPUT element, when starting from the KNOWN TEXT VALUE???

<span id="RandomlyGenerated35673">
	<span id="RandomlyGeneratedNum58532">
		<span id="RandomlyGenerated78539">
			<span/>
			<span/>
			<span id="RandomlyGenerated78539">KNOWN TEXT VALUE</span>
		</span>
	</span>
	<input class="GENERIC-NON-UNIQUE" type="button" value="GENERIC-NON-UNIQUE"/>
</span>

Answers


alecxe February 2016

You can use the following axis:

//span[. = 'KNOWN TEXT VALUE']/following::input[@type = 'button']

Post Status

Asked in February 2016
Viewed 1,767 times
Voted 14
Answered 1 times

Search




Leave an answer