我撰寫了一個 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)
uj5u.com熱心網友回復:
您的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)
# ^^^^^^^^^^^
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/425778.html
