我不知道如何正确循环所有这些而不会出错。
当它低于8000hp时,它将在控制台中显示“退出代码为0的过程完成”,这很奇怪,因为它永远都不应停止运行...
基本上,如果> = 8000生命值+以确保满足敌人等级+战斗值条件(敌人等级<= 1和BV <= 51),我需要它无限循环进行攻击
如果敌人等级或BV为> =,则应“再次搜索”
如果健康<= 8000,它应该入睡,直到有足够的战斗机会为止
这是一个可以自己测试的测试帐户:
用户:testaccount123 通过:python123 游戏:https://s9-en.bitefight.gameforge.com/user/login
from selenium import webdriver
import time
import re
driver = webdriver.Chrome("C:\\Users\\JC
WIN10\\PycharmProjects\\BitefightPvpBOT\\drivers\\chromedriver.exe")
driver.set_page_load_timeout(10)
driver.get("https://s9-en.bitefight.gameforge.com/user/login") # SERVER
LOGIN PAGE
driver.find_element_by_name("user").send_keys("testaccount123") # USERNAME
time.sleep(1)
driver.find_element_by_name("pass").send_keys("python123") # PASSWORD
time.sleep(1)
driver.find_element_by_class_name("btn-small").click() # LOGIN BUTTON
time.sleep(1)
driver.get("https://s9-en.bitefight.gameforge.com/robbery/index") # HUNT PAGE
time.sleep(1)
driver.find_element_by_name("optionsearch").click() # Werewolf Hunt
time.sleep(2)
enemyLevel = int(driver.find_element_by_xpath('//td[.="Level:"]/following-
sibling::td').text)
BV = int(driver.find_element_by_xpath('//td[.="Battle value:"]/following-
sibling::td').text)
elem = driver.find_element_by_xpath('//div[@class="gold"]')
HP = re.search(r'(\d+\.\d+)\s+/', elem.text).group(1)
HP = int(HP.replace('.', ''))
while enemyLevel <= 1 and BV <= 51 and HP >= 8000:
driver.find_element_by_xpath("//form/div/div/button").click() #Attack
time.sleep(2)
BattleReport = driver.find_element_by_id("reportResult") # BATTLE REPORT
print(BattleReport.text) # OUTPUTS BATTLE REPORT
time.sleep(2)
print("------ ------")
driver.get("https://s9-en.bitefight.gameforge.com/robbery/index")
driver.find_element_by_name("optionsearch").click() # Werewolf Hunt
time.sleep(2)
else:
driver.find_element_by_name("optionsearch").click()
print("Finding new victim")
如您所知,我在循环中非常糟糕。我将如何最好地应对?我尝试了一个while循环,但除非丢失了某些内容,否则无法使其正常工作。
感谢和亲切问候
答案 0 :(得分:2)
您的问题是您的else
将您踢出了循环。重构为while True
,可以使程序保持运行状态。因此,请在下面复制很多代码以获取完整的代码:
from selenium import webdriver
import time
import re
# login to your server here
# and set up driver
def get_new_enemy_stats(driver):
# go to page to find enemy
driver.get("https://s9-en.bitefight.gameforge.com/robbery/index") # HUNT PAGE
time.sleep(1)
driver.find_element_by_name("optionsearch").click() # Werewolf Hunt
time.sleep(2)
# get stats
enemyLevel = int(driver.find_element_by_xpath('//td[.="Level:"]/following-
sibling::td').text)
BV = int(driver.find_element_by_xpath('//td[.="Battle value:"]/following-
sibling::td').text)
# return them
return enemyLevel, BV
def get_health(driver):
elem = driver.find_element_by_xpath('//div[@class="gold"]')
HP = re.search(r'(\d+\.\d+)\s+/', elem.text).group(1)
HP = int(HP.replace('.', ''))
return HP
while True:
enemyLevel, BV = get_new_enemy_stats(driver)
if enemyLevel > 1 or BV >51:
# The stats are too high
continue
# do the attack
HP = get_hp(driver)
if HP<8000:
# sleep_some
所以我要做的是将敌人的统计信息添加到函数中。这使您的代码更加紧凑,并且很多更具可读性。您想在需要新敌人时获得新的统计数据。
返回统计信息,检查enemyLevel
和BP
。如果这些测试失败,则continue
将跳过其余代码,并进行while
的另一次迭代。您可以在最后检查HP
,如果它太低,您可以睡觉
else
循环中的 while
不在循环中 。它的功能类似于“在此期间执行此操作,否则执行其他操作”。这就是为什么您的循环退出并且脚本将完成的原因