Selecting an element by XPath for Selenium Web Scraping

6

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?

Share
Improve this question
New contributor
Edmund De Morcer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • I think it's better that you change the Accepted answer to Prophet's - see all comments below. – Ofer Zelig 3 hours ago

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue