我有一个代码,自动cookie点击器,但我不断得到一个错误,代码是:
def cookieclicker():
global driver
COOKIE = driver.find_element(By.ID, "bigCookie")
for i in range(500000000):
if keyboard.is_pressed('q') == False:
COOKIE.click()
products = [driver.find_element(By.ID, "productPrice" + str(i)) for i in range(0, 18, 1)]
for item in products:
cookie_count = driver.find_element(By.ID, "cookies")
count = int(cookie_count.text.split(" ")[0].replace(',', ''))
value = int(item.text.replace(',', ''))
if count >= value:
action = ActionChains(driver)
action.click(item)
action.perform()
cookieclicker()
我得到的错误是:
value = int(item.text.replace(',', ''))
ValueError: invalid literal for int() with base 10: ''
有人知道如何解决这个问题吗?
似乎你得到空字符串-所以你可以使用if/else
检查它之前int()
value = item.text.replace(',', '')
if value:
value = int(value)
if count >= value:
action = ActionChains(driver)
action.click(item)
action.perform()
或者您可以使用try/except
来捕获错误
try:
value = int(item.text.replace(',', ''))
if count >= value:
action = ActionChains(driver)
action.click(item)
action.perform()
except ValueError as ex:
print("Exception:", ex)