首页 > 解决方案 > 我收到错误:如果我更新购物车,则无法将字符串转换为浮点数

问题描述

重建步骤:

1.Go to http://practice.automationtesting.in/
2.Click on add to basket
3.Then view basket
4.Change the quantity to 2
5.Click on Update Basket
6.Save price before coupon
6.Use coupon code "krishnasakinala"
7.Click on Apply Coupon button
8.save price after coupon is applied
9. strip both before and after coupon price of rupee symbol
10.convert to float and assert pricebeforecoupon > priceaftercoupon

代码:

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E

driver: WebDriver = webdriver.Chrome(executable_path=r"C:\Users\Ratna 
Sinha\Downloads\chromedriver_win32\Chromedriver.exe")
driver.maximize_window()
url= "http://practice.automationtesting.in"
driver.get(url)
wait_timeout = 20
wait_variable = W(driver,wait_timeout)

driver.find_element_by_xpath("//a[@data-product_id='160']").click()
wait_variable.until(E.presence_of_element_located((By.XPATH,"//a[@class = 'added_to_cart wc-forward']"))).click()
wait_variable.until(E.presence_of_element_located((By.XPATH,"//input[@type='number']"))).clear()
wait_variable.until(E.presence_of_element_located((By.XPATH,"//input[@type='number']"))).send_keys(2)
wait_variable.until(E.element_to_be_clickable((By.NAME, "update_cart"))).click()
wait_variable.until(E.presence_of_element_located((By.CSS_SELECTOR,'[class="blockUI blockOverlay"]')))
wait_variable.until(E.staleness_of( driver.find_element_by_css_selector('[class="blockUI blockOverlay"]')))
pricebefcoupon = driver.find_element_by_xpath("//strong/span").text
driver.find_element_by_name("coupon_code").send_keys("krishnasakinala")
driver.find_element_by_name("apply_coupon").click()
wait_variable.until(E.presence_of_element_located((By.XPATH,"//div[@class='cart-collaterals']/div/div[@class ='blockUI blockOverlay']")))
wait_variable.until(E.staleness_of(driver.find_element_by_xpath("//div[@class='cart-collaterals']/div/div[@class ='blockUI blockOverlay']")))
priceafcoupon = driver.find_element_by_xpath("//strong/span").text
str = pricebefcoupon
str = str.strip('₹')
print(str)
str1 = priceafcoupon
str1 = str1.strip('₹')
print(str1)
assert float(str1) < float(str)

但是,如果我没有通过注释掉以下代码来更新购物车,它工作正常。

wait_variable.until(E.presence_of_element_located((By.XPATH,"//input[@type='number']"))).clear()
wait_variable.until(E.presence_of_element_located((By.XPATH,"//input[@type='number']"))).send_keys(2)
wait_variable.until(E.element_to_be_clickable((By.NAME, "update_cart"))).click()
wait_variable.until(E.presence_of_element_located((By.CSS_SELECTOR,'[class="blockUI 
blockOverlay"]')))
wait_variable.until(E.staleness_of( driver.find_element_by_css_selector('[class="blockUI 
blockOverlay"]')))

标签: pythonseleniumselenium-webdriver

解决方案


问题是您的号码包含逗号和小数点。Python 不能那么容易地解析它。

正如此线程中所建议的那样,您可以用硬逗号替换逗号,也可以通过(示例str = str.replace(',','')正确处理本地化locale

如果你注释掉你的行,你不会得到那么高的价格,这就是问题似乎消失的原因,但它与这些行的代码没有任何关系

正如其他人指出的那样:请不要使用预定义的符号作为变量名。您当前正在str用您的代码覆盖该函数。如果您使用更多的口语变量,也将更容易理解您的代码应该做什么。


推荐阅读