Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ValueError:无法将字符串转换为浮点:'';在python 3中_Python_Python 3.x_Selenium_Split_Floating Point - Fatal编程技术网

ValueError:无法将字符串转换为浮点:'';在python 3中

ValueError:无法将字符串转换为浮点:'';在python 3中,python,python-3.x,selenium,split,floating-point,Python,Python 3.x,Selenium,Split,Floating Point,所以我正在用python编写一个小机器人,我遇到了一个问题。这似乎是一个常见的问题,但我从未见过在我所处的相同情况下问这个问题 好的,下面是引起问题的代码: 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)

所以我正在用python编写一个小机器人,我遇到了一个问题。这似乎是一个常见的问题,但我从未见过在我所处的相同情况下问这个问题

好的,下面是引起问题的代码:

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=“draggablenavRightResizeable”]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span')。text是一个用于获取网站价值的selenium工具。正如您稍后将看到的,检索到的元素是一个数字,它应该与float()一起使用 “remove prc”是我创建的一个小函数,用于删除一个数字的百分比,如下所示:

def removeprc(string): #removes the % from a string 
    string = str(string)
    list = string.split('%')
    string = " ".join(list)

    return string
这可能不是最好的方法,但是当我单独测试它时,它是有效的

无论如何,当我运行我的全部代码时,我得到的是

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: ''
它输出-0.06,工作正常

所以我真的被困在这里了。它应该能工作,但不能。甚至最糟糕的是,它有时也会毫无理由地起作用。当我隔离问题时,它工作正常

任何帮助都将不胜感激


哦,如果您想查看整个代码,它在这里:

您在这里使用了关键字作为变量。我想这就是为什么有时候它不起作用的原因。 与str()类似,list()是一种将变量转换为列表的方法。我想,试着像下面这样重命名变量

def removeprc(string): #removes the % from a string 
    string = str(string)
    l = string.split('%')
    string = " ".join(l)
    return string
此错误消息

ValueError: could not convert string to float: ''
…表示Python解释器无法将字符串转换为浮点


你离得够近了。text方法将返回一个字符串并去除
%
,而不是
string.split('%')
您想要的
list=string.split('%')[0]

例如:

my_percentage = "99%"
my_string_num = my_percentage.split("%")[0]
print(my_string_num)
印刷品:

99
此外,
find\u element\u by\u xpath()
将只标识一个元素,使用文本您将获得一个字符串,因此
string=“”.join(list)
似乎是多余的

因此,为了有效地去除
%
,将字符串转换为float并打印,您的有效代码行将是:

print(float(browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text.split("%")[0]))

更新 当调用代码行时,您仍然会看到错误,因为具有所需文本的元素没有在DOM中呈现。作为一种解决方案,您需要为位于()的元素的可见性引入WebDriverWait,您可以使用以下方法:

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

返回的文本是空字符串,因此无法将其转换为
float
。加支票

b = removeprc(a)
if b:
    print(float(b))
else:
    print('b is an empty string')

不幸的是,它仍然不起作用:(
Traceback(最后一次调用):文件“C:\Users\pc adam\Documents\EISTI\algoprog\perso\python\fichiers\btc\ETHtradingbotV1.py”,第134行,在old_values=float(browser.find_element_by_xpath('/*[@id=“DragableNavRightResizeable”]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span')。text.split(“%”[0])ValueError:无法将字符串转换为float:“”
@AdamLeRoux签出更新的答案并让我知道状态ValueError:无法将字符串转换为float:“”:错误表明,您尝试将空字符串转换为
float
。请添加一个条件,以便对空字符串执行操作。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
b = removeprc(a)
if b:
    print(float(b))
else:
    print('b is an empty string')