Tomer
Tomer

Reputation: 3

xpath on the site is constantly changing

Peace. I registered a test on the amazon site. Doing a search of 11 iphone and then coming to a page of full products i choose first but its xpath

// span [contains (text (), 'Apple iPhone 11 (64GB) - Black')]

The problem is that I can use this xpath but tomorrow the xpath will be renamed because the first product is changed for example:

// span [contains (text (), 'Apple iPhone 11 Pro (64GB) - Space Gray')]

But I always choose the first product among all iphones even when the product changes? Thanks.

This is the page

https://www.amazon.co.uk/s?k=iphone+11&crid=3GCCCW0Q2Z1MQ&sprefix=iph%2Caps%2C220&ref=nb_sb_noss_2

Upvotes: 0

Views: 898

Answers (3)

Peter
Peter

Reputation: 898

You could use the class of the search item span:

//span[@class="a-size-medium a-color-base a-text-normal"]

Then if you can do:

first_iphone = driver.find_element_by_xpath('//span[@class="a-size-medium a-color-base a-text-normal"]')

Although all search items are all the same class, (in this case a-size-medium a-color-base a-text-normal) the find_element_by_xpath method will only look for the first one.

Upvotes: 0

prakasht
prakasht

Reputation: 478

Always try to find something on the page that is very unlikely to change. If the element that you're looking for doesn't have such properties, look at it's ancestors.

For example, in this case, you can see that one of this span's ancestors have cel_widget_id="MAIN-SEARCH_RESULTS" which'll most likely remain constant. So, the following xpath:

//span[@cel_widget_id="MAIN-SEARCH_RESULTS"]//h2/a/span

will give you all such titles. You can get the first index as

(//span[@cel_widget_id="MAIN-SEARCH_RESULTS"]//h2/a/span)[1]

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Use index and following xpath to get the first element.

(//a[@class='a-link-normal a-text-normal']/span)[1]

Upvotes: 1

Related Questions