Below is the part where my code program meets a runtime ValueError.
JavaScript
x
5
1
while real_original_amount > 1:
2
future_amount=driver.find_element_by_xpath('//[@id="app"]/div[1]/div[2]/div[1]/div/div[3]/div[2]/div[3]/div/div/span/span')
3
future_amount_float = float(future_amount.text)
4
betting_count = ()
5
As I’m using selenium as part of the program, im taking webelements into a variable then convert the variable to a float number as it is on the website. Below is the error:
JavaScript
1
5
1
Traceback (most recent call last):
2
File "C:UsersUserPycharmProjectsSelenium Projectsmain.py", line 74, in <module>
3
future_amount_float = float(future_amount.text)
4
ValueError: could not convert string to float: ''
5
Tried several different methods to solve this ValueError but all didn’t really worked out.
Advertisement
Answer
This error message…
JavaScript
1
2
1
ValueError: could not convert string to float: ''
2
…implies that the variable ' '
cannot be converted to float as it is a blank.
Solution
Possibly the textContext
wasn’t rendered when the line of code was executed. So to locate the element you need to induce WebDriverWait for the visibility_of_element_located()
and you can use the following Locator Strategy:
Using
XPATH
:JavaScript131future_amount = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//[@id="app"]/div[1]/div[2]/div[1]/div/div[3]/div[2]/div[3]/div/div/span/span")))
2future_amount_float = float(future_amount.text)
3
Note : You have to add the following imports :
JavaScript141from selenium.webdriver.support.ui import WebDriverWait
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support import expected_conditions as EC
4