【问题标题】:Play 2048 using selenium使用硒播放 2048
【发布时间】:2016-07-13 20:02:55
【问题描述】:

我已经编写了将播放 2048 直到游戏结束的代码,但是有没有办法让 selenium 在弹出时单击重试按钮?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get('https://gabrielecirulli.github.io/2048/')

grid = browser.find_element_by_tag_name('body')
direction = {0: Keys.UP, 1: Keys.RIGHT, 2: Keys.DOWN, 3: Keys.LEFT}
count = 0
browser.find_element_by_class_name('grid-container').click()
while True:
    count += 1
    grid.send_keys(direction[count % 4])
    try:
        WebDriverWait(browser, .00001).until(
            EC.presence_of_element_located((By.ID, "game-message game-over")))
        browser.find_element_by_class_name('game-over').click()
    except:
        print("OK")

【问题讨论】:

    标签: python python-3.x selenium


    【解决方案1】:

    快速玩游戏后,重试按钮看起来是:

    <a class="retry-button">Try again</a>
    

    所以

    browser.find_element_by_class_name('retry-button').click()
    

    应该适合你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多