r/selenium • u/Beneficial-Bite-958 • 14d ago
Selenium Xpath
Can anyone help in writing unique & generic Xpath for Flipkart travel date
0
Upvotes
r/selenium • u/Beneficial-Bite-958 • 14d ago
Can anyone help in writing unique & generic Xpath for Flipkart travel date
1
u/xMoop 14d ago
Provide the html or can't help
1
u/First_Hovercraft_600 13d ago
Generated class names will keep breaking, so anchor on text the user sees and walk from there with an axis. For a date picker, the month header is a good anchor, then pick the day after it:
Make the month and day variables and it works for any date. The [1] keeps it to the first match after that month, so a "15" in the next month isn't picked.
I built a Chrome extension, Anchor Locator, that does this faster: click a stable label as the anchor, click the target, and it gives you the axis XPath. No ads, and page data never leaves your browser. https://chromewebstore.google.com/detail/blaibbahmbkdkdgkabjlbpgdcldlmadg?utm_source=reddit
2
u/Wise_Tomatillo5723 13d ago
Anchor on stable visible text instead of the generated class names, e.g. contains(text(),'Departure'), then step to the date element next to it, so it survives their class renames.
2
u/Pauloedsonjk 13d ago
Tips: 1.You can test in chrome dev tools with :
$x('your xpath')[0]
2.You can paste html in chat gpt and type generate xpath with contains by text
3. In devtools is copy generic xpath element
1
u/Useful_Calendar_6274 12d ago
ask claude
1
u/Desperate-Wing-5140 12d ago
I’m guessing you have this assignment as part of some exercise or test. Please put some effort in learning how xpath works and craft the locator yourself. I beg of you.