使用硒玩2048

时间:2016-07-13 20:02:55

标签: python python-3.x selenium

我编写的代码将会播放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")

1 个答案:

答案 0 :(得分:1)

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

var connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionName1"];
var conString = connection.ConnectionString; // you can also get the provider
var myEntities = new MyEntities(conString);

所以

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

应该适合你