Nenad Nenad - 6 days ago 5
Python Question

Making a cookie clicker bot

I am making a program that opens up cookie clicker website and automatically clicks the cookie and that part works fine. Then I tried to add the feature that every time you have enough cookies to buy and upgrade, the program buys it or prints out "You don't have enough cookies to buy that!" But for some reason the program doesn't work. It clicks the cookie just fine but, even when you do have enough cookies to buy an upgrade it prints out message saying "You don't have enough cookies to buy that!". Here is my source code please let me know your ideas about how ti fix this program.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Firefox()
browser.get('http://orteil.dashnet.org/cookieclicker/')
cookie = browser.find_element_by_id('bigCookie')
while True:
cookie.click()
try:
product = browser.find_element_by_class_name('product unlocked enabled')
product.click()
except NoSuchElementException:
print('You don't have enough cookies to buy that!')
continue


P.s
This is the game if you don't know what I am talking about: http://orteil.dashnet.org/cookieclicker/

Answer

It's because find_element_by_class_name converts name to css selector. So if your name is "product unlocked enabled", selector will be ".product unlocked enabled". But right css selector for this case is ".product.unlocked.enabled" because this element have multiple class name which consists from 3 class names: 'product', 'enabled', 'unlocked'. You should use name like "product.unlocked.enabled" or for example "unlocked.enabled" which also correct because this element still have classes "unlocked" and "enabled".

Comments