我正在使用硒chromedriver为cookieclickerbot编写脚本。 问题在于它既不单击Cookie也不进行升级。 我已经玩了大约两天的代码,但是我对类的经验并不丰富。 请测试一下,看看是否可以解决,不要担心以不同的方式安排课程,我只想看看我做错了什么。 希望您也能对我进行解释,以便我可以学习更多有关Selenium,python和类的信息。
from selenium import webdriver
from time import sleep
class COOKIEBOT2000(object):
def __init__(self,upgrade=1,click=1,prints=1):
self.upgrade = upgrade
self.click = click
self.prints = prints
self.cursor = 0
self.grandma = 0
self.farm = 0
self.mine = 0
self.factory = 0
self.temple = 0
self.wizard = 0
self.driver = webdriver.Chrome ("Path too chromedriver")
def Clicker(self):
try:
if self.click == 1:
if self.prints == 1:
print ("clicking")
print(self.driver)
i = 0
while i < 11:
click = driver.find_element_by_id("bigCookie")
click.click()
i = i + 1
print (i)
if self.prints == 1:
click = driver.find_element_by_id("cookies")
click.click()
click = click.text
money = click.split("\n")
money = money[0].split(" ")
money = int(money[0])
print (money)
self.Upgrader()
except:
self.Upgrader()
def Upgrader(self):
if self.upgrade == 1:
if self.prints == 1:
print ("Upgrading")
try:
click = driver.self.find_element_by_id("product0")
if click.get_attribute("class") == "product unlocked enabled":
if self.cursor < 51:
click.click()
self.cursor = self.cursor + 1
click = driver.self.find_element_by_id("product1")
if click.get_attribute("class") == "product unlocked enabled":
if self.grandma < 7:
click.click()
self.grandma = self.grandma + 1
click = driver.self.find_element_by_id("product2")
if click.get_attribute("class") == "product unlocked enabled":
if self.farm < 41:
click.click()
self.farm = self.farm + 1
click = driver.self.find_element_by_id("product3")
if click.get_attribute("class") == "product unlocked enabled":
if self.mine < 41:
click.click()
self.mine = self.mine + 1
click = driver.self.find_element_by_id("product4")
if click.get_attribute("class") == "product unlocked enabled":
if self.factory < 51:
click.click()
self.factory = self.factory + 1
click = driver.self.find_element_by_id("product5")
if click.get_attribute("class") == "product unlocked enabled":
if self.temple < 51:
click.click()
self.temple = self.temple + 1
click = driver.self.find_element_by_id("product6")
if click.get_attribute("class") == "product unlocked enabled":
if self.wizard < 51:
click.click()
self.wizard = self.wizard + 1
self.Clicker()
except:
self.Clicker()
return
def Start(self):
self.driver.get("http://orteil.dashnet.org/cookieclicker/")
sleep(1)
if self.prints == 1:
print("begun")
self.Clicker()
CK = COOKIEBOT2000()
CK.Start()
答案 0 :(得分:1)
在您的Clicker
函数中,替换掉this的每个实例-
driver.find_element_by_id
由此-
self.driver.find_element_by_id
然后,在您的Upgrader
函数中,替换this-
driver.self.find_element_by_id
由此-
self.driver.find_element_by_id
这样做会使脚本作为游戏的一部分连续执行所有点击。