7

I am trying to select a 'price' from a web page using Selenium (Python). The logic behind the web page is that there are two types of prices - 'regular price' and 'sale price' and they have different XPaths.

The conundrum I have is that I am able to select the parent element which contains both 'regular price' and 'sale price' (that is, if sale price exists for a particular product). I then try to apply a 'Try' and 'Except' to it, so that if the 'sale price' exists - grab that price, otherwise - grab the 'regular price'. However, I do not appear to be able to force it to only look for a 'sale price' within the selected parent element, rather it starts from the beginning of the page with the result that if there is a single 'sale price' on the page -it would apply it to all products.

I have set out the relevant extractors of the code plus the HTML below.

prices = self.driver.find_elements_by_xpath('//*[contains(@class,"row item-block checkout-item isc-productContainer")]')
all_prices_list=[]
for i in prices:
 try:
 sale_price=self.driver.find_element_by_xpath('.//*[contains(@class,"row item-block checkout-item isc-productContainer")]/div[4]/isc-product-price-pdp/span/span/span[1]/span[2]')
 all_prices_list.append(sale_price.text)
 except:
 reg_price=self.driver.find_element_by_xpath('.//*[contains(@class,"row item-block checkout-item isc-productContainer")]/div[4]/isc-product-price-pdp/span/span/span[2]')
 all_prices_list.append(reg_price.text)

The relevant snippet of the HTML code is as follows.

<span class="sale-price" id="salePrice">
 <span class="price-title">Sale</span>
 <span class="price-sale" ng-bind="vm.getActualPrice(product)">0ドル.98</span>
 </span>
<span class="regPrice" id="regPrice">
 <span class="price-title">Reg:</span>
 <span class="price-old" ng-bind="vm.getRegularPrice(product)">0ドル.99</span>
 </span>

There is a a way to only look for 'regular' or 'sale prices' within the selected 'prices' element?

Prophet
33.5k29 gold badges58 silver badges90 bronze badges
asked May 8, 2021 at 21:03
0

3 Answers 3

29

Looks like you are attempting to find sale_price and reg_price inside the parent element but actually not doing so.
So, try the following:

prices = self.driver.find_elements_by_xpath('//*[contains(@class,"row item-block checkout-item isc-productContainer")]')
all_prices_list=[]
for i in prices:
 try:
 sale_price=i.find_element_by_xpath('.//*[contains(@class,"row item-block checkout-item isc-productContainer")]/div[4]/isc-product-price-pdp/span/span/span[1]/span[2]')
 all_prices_list.append(sale_price.text)
 except:
 reg_price=i.find_element_by_xpath('.//*[contains(@class,"row item-block checkout-item isc-productContainer")]/div[4]/isc-product-price-pdp/span/span/span[2]')
 all_prices_list.append(reg_price.text)

Additionally, your locators could be improved

Braiam
4,49511 gold badges50 silver badges83 bronze badges
answered May 8, 2021 at 21:15
Sign up to request clarification or add additional context in comments.

Comments

1

Here is an alternative solution:

using the index of the products
prices = driver.find_elements_by_xpath('//*[contains(@class,"row item-block checkout-item isc-productContainer")]')
all_prices_list = []
for i in range(0,len(prices)):
 try:
 sale_price = driver.find_elements_by_xpath("//span[@ng-bind='vm.getActualPrice(product)']")[i]
 all_prices_list.append(sale_price.text)
 except:
 reg_price = driver.find_elements_by_xpath("//span[@ng-bind='vm.getRegularPrice(product)']")[i]
 all_prices_list.append(reg_price.text)
print (all_prices_list)
Prophet
33.5k29 gold badges58 silver badges90 bronze badges
answered May 8, 2021 at 21:39

1 Comment

This is fantastic, when i opened the page, there was no answer, I didnt refresh the page to see that someone already posted. @edmund-de-morcer please make the other answer as the correct response.
0

Using the index of the products:

prices = driver.find_elements_by_xpath('//*[contains(@class,"row item-block checkout-item isc-productContainer")]')
all_prices_list = []
for i in range(0,len(prices)):
 try:
 sale_price = driver.find_elements_by_xpath("//span[@ng-bind='vm.getActualPrice(product)']")[i]
 all_prices_list.append(sale_price.text)
 except:
 reg_price = driver.find_elements_by_xpath("//span[@ng-bind='vm.getRegularPrice(product)']")[i]
 all_prices_list.append(reg_price.text)
print (all_prices_list)
Diego Borba
2,4356 gold badges15 silver badges29 bronze badges
answered Nov 13, 2023 at 14:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.