Problem with checking if prices are between two values (Selenium + Python)
URL = http://practice.automationtesting.in/shop/
I need to check if the prices of the books are between two values.
I tried to find all the prices:
prices = driver.find_elements_by_xpath("//*[@class='woocommerce-Price-amount amount']") for price in prices: x = price.text y = x.replace("₹", "") z = float(y) print(z)
I'm getting all the values, without "₹". Can someone give me some tips, what should I do next to find out if prices are between two values? Thanks.
If you want to have all prices in a given interval, you can do the following. If you want to do something else, you can just perform other operation instead of filtering.
prices_elements = find_prices(driver) prices = extract_prices_values(prices_elements) prices_between = filter_prices_not_in_interval(prices, 15, 150) def find_prices(driver): return driver.find_elements_by_xpath("//*[@class='woocommerce-Price-amount amount']") def extract_prices_values(prices_elements): return [ clean_price_string(price.text) for price in prices_elements ] def clean_price_string(price): return price.replace("₹", "") def filter_prices_not_in_interval(prices, lower, higher): return [ price for price in prices if float(price) > lower and float(price) < higher ]
With the finding and filtering functions above, it should be trivial if you want to just the webelement object that meet the criteria, which means you can do anything with the whole object.