簡體   English   中英

嘗試了幾種方法,但這個值錯誤不斷彈出與 WebDriver 一起運行

[英]Tried several methods but this Value Error keeps popping up running with the WebDriver

下面是我的代碼程序遇到運行時 ValueError 的部分。

while real_original_amount > 1:
    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')
    future_amount_float = float(future_amount.text)
    betting_count = ()

當我使用 selenium 作為程序的一部分時,我將 webelements 放入變量中,然后將變量轉換為網站上的浮點數。 以下是錯誤:

Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\Selenium Projects\main.py", line 74, in <module>
    future_amount_float = float(future_amount.text)
ValueError: could not convert string to float: ''

嘗試了幾種不同的方法來解決這個 ValueError 但都沒有真正解決。

此錯誤消息...

ValueError: could not convert string to float: ''

...意味着變量' '不能轉換為float因為它是空白


解決方案

執行代碼行時可能未呈現textContext 因此,要定位您需要為visibility_of_element_located()誘導WebDriverWait的元素,您可以使用以下定位器策略

  • 使用XPATH

     future_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"))) future_amount_float = float(future_amount.text)
  • 注意:您必須添加以下導入:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM