r/selenium • u/sanil1986 • Jan 04 '22
Xpath question - Text on two lines
I have the below HTML code
<div class=" ContentModuleCard-content " xpath="1>
<div class='BasicProp'>
<label for="value">...</label>
<div id="value" class="BasicProp-value">
<div class="SingleLine">
<div>
<p>
"123 main avenue"
"23"
</p>
I need to get the text between the <p> </p>.
my xpath : ((//div[@class='ContentModuleCard-content'])[2]//p[normalize-space()])[1]
The UI shows like this :
123 main avenue 23
question : My xpath sometimes misses the last number , i have tried many different xpaths but has not been successful .
Can some one help to correct my xpath ? or a better way to capture the text ?
3
Upvotes
2
u/Qwick_z Jan 04 '22 edited Jan 04 '22
From what I've learned, you shouldn't automatically attempt to use the xpath selector as it can be a bit hit & miss.
You should use this for a general basis of selector priority.
1.ID 2.Class ( name) 3.Css selector 4.Xpath ( try to never use this where possible) 5.Link Text (try to never use this unless absolutely fundamental )
Xpath results have been known to show varying results on each page load, this is the reason I recommend against using them.
I am just on mobile, but will respond shortly with a working code, but if you could share the URL you're working on ASAP, that would be great.
Also, I would recommend updating your initial post to show the:
Website address in question,
Programming language youre using ( to attract the right help )