Issue
I have the following question from an interview: Write an xpath to access the hotels link by traversing from flights link:
i have highlighted the flights link in web browsertool. but how to do after that i am not sure
Solution
for this HTML:
<ul class="makeFlex font12">
<li data-cy="menu_Flights" class="menu_Flights">
<a href="https://www.makemytrip.com/flights/" class="active makeFlex hrtlCenter column
">
<span class="chNavIcon appendBottom2 chSprite chFlights active"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Flights
</span>
</a>
</li>
<li data-cy="menu_Hotels" class="menu_Hotels">
<a href="https://www.makemytrip.com/hotels/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chHotels"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Hotels
</span>
</a>
</li>
<li data-cy="menu_Homestays" class="menu_Homestays">
<a href="https://www.makemytrip.com/homestays/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chHomestays"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Homestays
</span>
</a>
</li>
<li data-cy="menu_Holidays" class="removeItemMargin menu_Holidays">
<a href="https://www.makemytrip.com/holidays-india/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chHolidays"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Holiday Packages
</span>
</a>
</li>
<li data-cy="menu_Trains" class="menu_Trains">
<a href="https://www.makemytrip.com/railways/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chTrains"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Trains
</span>
</a>
</li>
<li data-cy="menu_Buses" class="menu_Buses">
<a href="https://www.makemytrip.com/bus-tickets/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chBuses"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Buses
</span>
</a>
</li>
<li data-cy="menu_Cabs" class="menu_Cabs">
<a href="https://www.makemytrip.com/cabs/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chCabs"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Cabs
</span>
</a>
</li>
<li data-cy="menu_Visa" class="menu_Visa">
<a href="https://www.makemytrip.com/visa/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chVisa"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Visa
</span>
</a>
</li>
<li data-cy="menu_Charters" class="menu_Charters">
<a href="https://www.makemytrip.com/charter-flights/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chCharterFlights"></span>
<span class="reduceMenuSpacing chNavText darkGreyText">
<!-- --> <!-- -->Charter Flights
</span>
</a>
</li>
<li data-cy="menu_Activities" class="menu_Activities">
<a href="https://www.makemytrip.com/activities/" class="makeFlex hrtlCenter column">
<span class="chNavIcon appendBottom2 chSprite chActivities"></span>
<span class="false chNavText darkGreyText">
<!-- --> <!-- -->Activities
</span>
</a>
</li>
</ul>
A simple xpath to access the hotels link
by traversing from flights link
:
//a[contains(@href,'/flights/')]/../following-sibling::li
Explanation:
//a[contains(@href,'/flights/')]
is to target flights
and then go step up in DOM by using /..
and the looking for following-sibling
which is li
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.