I'm coding a little bot in Python and I'm having a problem. It seems to be a common problem, but I've never seen it asked in the same situation I'm in.
Ok so here is the code posing problem:
old_values = float((removeprc(browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text)))
browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text)
is a selenium tool used to get a value of a website. as you will see later, the element retrieved is a number which should work with float()
"remove prc" is a little function I created to remove the % of a number, here it is:
def removeprc(string): #removes the % from a string
string = str(string)
list = string.split('%')
string = " ".join(list)
return string
It's probably not the best way to do it, but it works when I test it alone.
anyway, here is what I get when I run my entire code
loading page ...
page loaded
acquiring values ...
values acquired
running eth trade
-0.37
Traceback (most recent call last):
File "C:\Users\pc adam\Documents\EISTI\algoprog\perso\python\fichiers\btc\ETHtradingbotV1.py", line 138, in <module>
profit = float(browser.find_element_by_xpath('/html/body/div[3]/section[16]/section[2]/section[2]/section[2]/div/div[1]/table/tbody/tr/td[15]/span').text)
ValueError: could not convert string to float: ''
the first 5 lines are useless. on the 6th line, I printed what i am trying to get the float() of. As you can see, it should work and ... It does ! sometimes.
that's the weirdest thing about this, it works perfectly half the time! I've read on the internet that this can happen if you try to float() things that are not numbers or that have weird shit in them, like spaces. As you can see, I think it's not the case here.
When I try to isolate the problem by running a simplified version of the program like this :
a = "-0.06%"
def removeprc(string): #removes the % from a string
string = str(string)
list = string.split('%')
string = " ".join(list)
return string
b = float(removeprc(a))
print(b)
it outputs -0.06 and works perfectly ???
So I'm really stuck here. It should work, but it doesn't. Even worst, it works sometimes, for no reason. And when I isolate the problem, it works fine.
Any help would be extremely appreciated!
Oh and if you want to see the entire code, it's here: https://github.com/Madaxuorel/proj-ethTB/blob/master/ETHtradingbotV1.py