我将如何使用.text中的信息

时间:2018-11-16 03:33:57

标签: python selenium

因此,我创建了一段简单的代码,一直执行到整个过程,但是在输出战斗报告之后,我想使用Health(余役):NUMBER来决定是否循环返回提供的代码< / p>

driver.get("https://randomtextrpg.com") # Starting Page
time.sleep(1)

driver.find_element_by_name("optionsearch").click() # Find Monsters
time.sleep(1)

driver.find_element_by_xpath("//form/div/div/button").click() # Attack Monster
time.sleep(1)

BattleReport = driver.find_element_by_id("reportResult") # Battle Report
print (BattleReport.text)
time.sleep(1)

如您所见,在《战地报告》中,我告诉它打印结果……看起来像这样:

Battle Report 16.11.2018 03:21
Winner: Warlord

End of fight: The attackers have caused more damage (5655 : 4560)!

Health (after battle): 16.669

Warlord captured : 20 Gold + 509 Gold bonus + 2 Experience

如何使用“生命值(战斗后):HP”来创建循环?

我知道我想做什么,所以如果健康(战后)=> 8000继续,我想拥有它

但是如果<8000等待5分钟,则检查> 8000是否继续遍历该代码块。

亲切的问候

1 个答案:

答案 0 :(得分:0)

使用while循环示例

import re

def letBattle():
  driver.get("https://randomtextrpg.com") # Starting Page
  time.sleep(1)

  driver.find_element_by_name("optionsearch").click() # Find Monsters
  time.sleep(1)

  driver.find_element_by_xpath("//form/div/div/button").click() # Attack Monster
  time.sleep(1)

  BattleReport = driver.find_element_by_id("reportResult") # Battle Report
  print (BattleReport.text)

  result = re.search(r'battle\):\s+([\d\.]+)', BattleReport.text).group(1) # 16.669
  result = result.replace('.', '') # 16669
  return int(result)


health = 8888 # dummy for first Battle

while health >= 8000:
  health = letBattle()
  if health <= 8000:
    print('health under 8000, sleeping 5 minutes...')
    time.sleep(300)
    # do your check
    # health = doCheckHealth()
  else:
    print('Continuing battle, health is: ' + health)