I don't understand why the replace got unresolved attribute reference 'replace' for class 'int' error. And then when the money reach more than 1000, I got value error, even though I tried to handle it with exception by removing comma from the string :
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Day48_selenium_cookie_clicker\main.py", line 37, in <module>
money = int(cookies_owned.text)
ValueError: invalid literal for int() with base 10: '1,012'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Day48_selenium_cookie_clicker\main.py", line 50, in <module>
money = int(cookies_owned.text)
ValueError: invalid literal for int() with base 10: '1,012'
This is my code:
from selenium import webdriver
import time
from timeit import default_timer as timer
chrome_driver_path = "C:\Development\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("http://orteil.dashnet.org/experiments/cookie/")
timeout = 30 # [seconds]
time_check = time.time() 5
ids = driver.find_elements_by_xpath('//*[@id]') # find all ids within a webpage
ids_list = [i.get_attribute('id') for i in ids]
items_for_sale_ids = [i for i in ids_list if i[0] == "b" and i[1] == "u" and i[2] == "y"][:8]
items_data = [i.text.split("\n") for i in driver.find_elements_by_id("store")][0]
items_for_sale = [items_data[i].split(" - ") for i in range(len(items_data)) if i % 2 == 0]
price = [int(y[1].replace(",", "")) for y in items_for_sale]
items_pricelist = list(zip(items_for_sale_ids, price))
items_pricelist_dict = {i[1]: i[0] for i in items_pricelist}
start = timer()
cookie = driver.find_element_by_id("cookie")
while True:
cookie.click()
if time.time() > time_check:
# print("hello")
cookies_owned = driver.find_element_by_id("money")
try:
money = int(cookies_owned.text)
affordable_upgrades = {}
for cost, id in items_pricelist_dict.items():
if money > cost:
affordable_upgrades[cost] = id
max_upgrade = max(affordable_upgrades)
print(max_upgrade)
to_purchase_id = affordable_upgrades[max_upgrade]
to_buy = driver.find_element_by_id((to_purchase_id))
to_buy.click()
time_check = time.time() 5
except ValueError:
money = int(cookies_owned.text)
formatted_money = money.replace(",", "")
affordable_upgrades = {}
for cost, id in items_pricelist_dict.items():
if formatted_money > cost:
affordable_upgrades[cost] = id
max_upgrade = max(affordable_upgrades)
print(max_upgrade)
to_purchase_id = affordable_upgrades[max_upgrade]
to_buy = driver.find_element_by_id((to_purchase_id))
to_buy.click()
time_check = time.time() 5
CodePudding user response:
see below.
(the problem is the ,
in value. once we remove it - it works)
value = '1,012'
try:
int_value = int(value)
except ValueError:
print('failed - lets try again..')
int_value = int(value.replace(',',''))
print(int_value)
output
failed - lets try again..
1012
CodePudding user response:
I got value error, even though I tried to handle it with exception by removing comma from the string :
try:
money = int(cookies_owned.text)
...
except ValueError:
money = int(cookies_owned.text)
...
Yes, you handle Exception and after that raise same error again ;)
Correct code is:
try:
money = int(cookies_owned.text)
...
except ValueError:
money = int(cookies_owned.text.replace(",", ""))
...
Or simpler:
try:
money = int(cookies_owned.text.replace(",", ""))
...
except ...
Here is no problem to replace ,
anytime. You don't need to wait for exception. replace(",", "")
will be correct for any number (1
, 100
, 1,000
, 1,000,000
, ...)