This is a inverse question of
$driver.get_element_by_xpath(....)
Background
I have a series of dynamically generated pages to parse. The target element doesn't have a good locator, xpath, text, or id. However, the neighbor element has a unique text to match. My plan is to locate the neighbor element and use its xpath to come up with the target xpath.
$neighbor_element = $driver.get_element_by_text("unique text"); # or some other way
$neighbor_xpath = $neighbor_element.xpath; # this step is the question
$target_xpath = modify($neighbor_xpath); # this is my function
$target_element = $driver.get_element_by_xpath($target_xpath);
I have searched around. cannot find a function or method to get xpath from an element, Perl or Python.
Update
I apologize that I cannot post the example page because it is owned by a company, but I hope the question is straightforward enough. When I use chrome devtools to inspect, I see the xpath are related
neighbor xpath = //*[@id="lable_ni.dynmic_string 123456"]/lable/span[2]
target xpath = //*[@id="dynmic_string 123456"]
Update2
This the dummy test file
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<div id="lable_ni.dynmic_string 123456">
<label id="lable_ni.dynmic_string 123456">
<!-- I use this as neighbour element -->
<span>unique text</span>
</label>
</div>
<div>
<!-- target element is here -->
<textarea id="dynmic_string 123456">target text is here</textarea>
</div>
</div>
</body>
</html>
This is how it looks like
My strategy was to
- find the xpath of "unique text". (this text is known beforehand)
- convert it to the xpath of target text
- get the target text
I stuck at the first step
` and there's another `
` right under/above (etc) it in source?
– zdim Apr 12 '22 at 21:19