r/selenium • • 13d ago

Selenium Xpath

Can anyone help in writing unique & generic Xpath for Flipkart travel date

0 Upvotes

8 comments sorted by

1

u/xMoop 13d ago

Provide the html or can't help

1

u/Beneficial-Bite-958 13d ago

I can share the link but not sure if we can share complete html dom since it will be so lengthy 

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:

//*[normalize-space()='November 2026']/following::*[normalize-space()='15'][1]

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

1

u/Beneficial-Bite-958 13d ago

I know by using index we can easily find, but wanted know is there anything else without using index

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/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.