首页 > 解决方案 > 将密钥发送到游戏容器 2048

问题描述

我正在尝试将密钥发送到 2048,并且我正在尝试等到找到游戏容器元素然后将密钥发送给它。由于某种原因 selenium 无法找到游戏容器。我假设这与关注正确的元素有关,但我无法弄清楚。

WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, r'/html/body/div[1]/div[4]'))).send_keys(Keys.UP)

这会引发超时异常,因为它无法找到游戏容器。有人可以帮我将箭头键发送到 2048 吗?我在https://play2048.co/上。

标签: pythonhtmlautomationselenium-chromedriver

解决方案


试试看:

from selenium.webdriver.common.keys import Keys
import random
import time

moves = [Keys.LEFT, Keys.DOWN, Keys.RIGHT, Keys.UP]
while True:
    driver.find_element_by_css_selector('body').send_keys(random.choice(moves))
    time.sleep(2)

python:如何随机按箭头键硒


推荐阅读