Robot Marvin 42
Robot Marvin 42

Reputation: 37

Selecting price using Selenium and Python with changing classes

I'm pulling rates from a site by navigating to different listings, but the class is not constistant between listings:

Listing 1:

<div class="jss238" data-lpos="price-widget">
 <div class="jss57 jss277 jss268">
 $329.00
 <span class="jss269">/Night</span>
 </div>
</div>

Listing 2:

<div class="jss501" data-lpos="price-widget">
 <div class="jss57 jss540 jss531">
 $250.00
 <span class="jss269">/Night</span>
 </div>
</div>

The class changes for each listing. From what I've been learning, I can select by class but it's not consistent between listings. Can I use "price-widget" as a starting point?

Upvotes: 0

Views: 156

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

Yes you can.

use the below XPATH :

//span[contains(text(), '/Night')]/preceding-sibling::div

take everything in a list and iterate over that and get your desired element. like below :

price_list = driver.find_elements(By.XPATH, "//span[contains(text(), '/Night')]/preceding-sibling::div")
for price in price_list: 
   print(price.text)

Upvotes: 1

Prophet
Prophet

Reputation: 33361

To get all the prices you can do the following:

prices = driver.find_elements_by_xpath('//div[@data-lpos="price-widget"]')
for price in prices:
    print(price.text.strip())

You can use css selector as well. In that case it will be:

prices = driver.find_elements_css_selectro('div[data-lpos="price-widget"]')

I used strip() to remove leading and trailing spaces from the string.
In case you want to remove the $ sign from the string use .replace("$", ""), so it will be

prices = driver.find_elements_by_xpath('//div[@data-lpos="price-widget"]')
for price in prices:
    print(price.text.strip().replace("$", ""))

Upvotes: 1

Related Questions