Selenium Webdriver 模块:Keys.UP 未按预期工作

Selenium Webdriver module: Keys.UP does not work as expected

我写了一个 python 脚本,应该可以自动玩游戏 2048 (https://play2048.co/)。

问题是:浏览器似乎忽略了击键。或者程序运行速度太快,无法让浏览器点击游戏。我已经检查了 Selenium 文档,但不确定是否必须包含一些明确的等待。

这是我的代码:

#! python3
#_2048.py - A program that plays the game 2048 (https://play2048.co/)
# automatically.

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

def assess_board():
    tileContainer = browser.find_element(By.CLASS_NAME, 'tile-container') # finds the 
tile container
    tiles = tileContainer.find_elements(By.TAG_NAME, "div[class*='tile-position']") # 
finds every tile
    for tile in tiles: # maps all values in the board
        boardvalues.append(tile.get_attribute('class')[10])

def click(): # Performs a click (changes after every click: UP-RIGHT-DOWN-LEFT)
    if start % 4 == 1:
        print('up')
        Keys.UP
    elif start % 4 == 2:
        print('right')
        Keys.RIGHT
    elif start % 4 == 3:
        print('down')
        Keys.DOWN
    elif start % 4 == 0:
        print('left')
        Keys.LEFT

browser = webdriver.Firefox() # Open Firefox
browser.get('https://play2048.co/') # Go to https://play2048.co/
wait = WebDriverWait(browser, 10)

boardvalues =[]
number_of_clicks = 0
start = 1
while '2048' not in boardvalues:
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".game-container")))
    click()
    number_of_clicks += 1
    start += 1
    assess_board()
    if len(boardvalues) == 16:
        print('You lost!')
        print('number of clicks: ', number_of_clicks)
print('2048! You won!')
print('number of clicks: ', number_of_clicks)

游戏的目标是通过按箭头键使方块相互滑动来达到 2048 分。当方格已满且您未达到 2048 格时游戏结束。

我不确定在我的 while 循环中发生了什么。浏览器似乎忽略了箭头键的击键。瓷砖没有移动。每次模拟单击箭头键都应该移动图块。

这可能是什么问题?

代码更新

#! python3
#_2048.py - A program that plays the game 2048 (https://play2048.co/)
# automatically.

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

def assess_board():
    tileContainer = browser.find_element(By.CLASS_NAME, 'tile-container') # finds the tile container
    tiles = tileContainer.find_elements(By.TAG_NAME, "div[class*='tile-position']") # finds every tile
    for tile in tiles: # maps all values in the board
        boardvalues.append(tile.get_attribute('class')[10])

def click(): # Performs a click (changes after every click: UP-RIGHT-DOWN-LEFT)
    if start % 4 == 1:
        print('up')
        bodyElem.send_keys(Keys.UP)
    elif start % 4 == 2:
        print('right')
        bodyElem.send_keys(Keys.RIGHT)
    elif start % 4 == 3:
        print('down')
        bodyElem.send_keys(Keys.DOWN)
    elif start % 4 == 0:
        print('left')
        bodyElem.send_keys(Keys.LEFT)

browser = webdriver.Firefox() # Open Firefox
browser.get('https://play2048.co/') # Go to https://play2048.co/
bodyElem = browser.find_element(By.TAG_NAME, 'body')
wait = WebDriverWait(browser, 10)

boardvalues =[]
number_of_clicks = 0
start = 1
while '2048' not in boardvalues:
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".game-container")))
    click()
    time.sleep(0.5)
    number_of_clicks += 1
    start += 1
    assess_board()
    if len(boardvalues) == 16:
        print('You lost!')
        print('number of clicks: ', number_of_clicks)
print('2048! You won!')
print('number of clicks: ', number_of_clicks)

您的 click() 函数实际上没有点击任何东西:

def click(): # Performs a click (changes after every click: UP-RIGHT-DOWN-LEFT)
    if start % 4 == 1:
        print('up')
        Keys.UP
    #   ^^^^^^
    ...

您此处的代码只是引用 Keys.UP 对象。相反,您需要找到一个元素并将该键发送给它:

def click(): # Performs a click (changes after every click: UP-RIGHT-DOWN-LEFT)
    # main <body> tag acts as your element that controls the keys
    board = browser.find_element_by_css_selector('body')
    if start % 4 == 1:
        print('up')
        board.send_keys(Keys.UP)
        # ^^^^^^^^^^^