I am using selenium to find a web element on a website that shows the temperature in this format "39 c". Is there anyway to convert that into int so I could use operators like <,=>,<, etc?
temperature = driver.find_element(By.ID, "temperature")
temperature = int(float(temperature.text))
if temperature < 19:
driver.find_element_by_link_text('Buy sunscreens').click()
elif temperature > 34:
driver.find_element_by_link_text('Buy moisturizers').click();
ValueError: could not convert string to float: '9 ℃'
CodePudding user response:
You could use str.split
:
temperature = int(temperature.text.split()[0])
Or use re
:
temperature = int(re.search('\d ', temperature.text)[0])
CodePudding user response:
You can split the string based on spaces, and then can call int
on it.
temperature = driver.find_element(By.ID, "temperature")
temperature_int = int(temperature.text.split(' ')[0])
print(type(temperature_int))
print(temperature_int)