为什么在'float'和'FirefoxWebElement'的实例之间不支持Typerror:'>'?

时间:2019-05-19 19:45:55

标签: python selenium selenium-webdriver

我正在尝试使用Python和Selenium编写脚本,以将value _metalvalue_crystalvalue_deutgauss_met_reqgauss_crys_req之间的数字进行比较,和gauss_deut_req

此错误不断弹出

  

回溯(最近通话最近):
    在<module>中的文件“ /home/badjorans/Desktop/stob/selenss.py”,第138行
      如果value_crystal> gauss_crys_req和value_metal> gauss_met_req和value_deuterium> gauss_deut_req:
  TypeError:“ float”和“ FirefoxWebElement”的实例之间不支持“>”

我试图将数字转换为浮点数,但错误不断出现。


metal = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[1]/ul/li[1]/span")
crystal = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[1]/ul/li[2]/span")
deuterium = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[1]/ul/li[3]/span")
blackmatter = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[1]/ul/li[5]/a/span")
energy = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[1]/ul/li[4]/span")

value_metal = float(metal.text)
value_deuterium = float(deuterium.text)
value_crystal = float(crystal.text)
value_energy = int(energy.text)

gauss_met_req = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[3]/div[2]/div[1]/form/div/div[2]/div[2]/ul/li[1]/div[2]")
print (gauss_met_req.text)
value_gauss_met = float(gauss_met_req.text)

gauss_crys_req = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[3]/div[2]/div[1]/form/div/div[2]/div[2]/ul/li[2]/div[2]")
print (gauss_crys_req.text)
value_gauss_crys = float(gauss_crys_req.text)

gauss_deut_req = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[3]/div[2]/div[1]/form/div/div[2]/div[2]/ul/li[3]/div[2]")
print (gauss_deut_req.text)
value_gauss_deut = float(gauss_deut_req.text)


print(value_metal)
print (value_crystal)
print(value_deuterium)

if value_crystal > gauss_crys_req and value_metal > gauss_met_req  and value_deuterium > gauss_deut_req :
    print ("we have enough resources to build Gauss Canon")

else:
    print ("NOT ENOUGH RESOURCES")

1 个答案:

答案 0 :(得分:1)

您没有像使用gauss_crys_reqgauss_met_reqgauss_deut_req那样将value_crystalvalue_metalvalue_deuterium转换为浮点数。您似乎已经知道如何从元素中获取文本并将其转换为浮点型,因此只需对要在条件中比较的所有变量使用相同的方法即可。