Home > Net >  Is there a way to convert a web element found by selenium into an integer? (Python)
Is there a way to convert a web element found by selenium into an integer? (Python)

Time:10-24

prices = driver.find_elements(By.CLASS_NAME, 'a-price-whole')
for i in range(len(prices)):
    num = float(prices[i].text)
    total_prices.append(num)

I'm trying to convert the elements found on a website by selenium into an integer or float in python but each time I get an error that says either "num = int(prices[i].text) ValueError: invalid literal for int() with base 10: ''" or "num = float(prices[i].text) ValueError: could not convert string to float: ''" when I try to convert the received elements into a float. How do I fix this problem?

CodePudding user response:

While fetching the prices, there is a comma(,) in every text, that's why you are getting that error. You have to replace that comma:

for i in range(len(prices)):
    num = prices[i].text.replace("," , "")
    total_prices.append(float(num))

print(total_prices)
  • Related