python - 将密钥发送到游戏容器 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/上。
解决方案
试试看:
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)
推荐阅读
- c# - 如何使用c#将文本框中输入的值与数组(mysql数据集)中的值顺序进行比较?
- python - 如何更有效地提取自定义类型的numpy数组的值?
- c# - 如何创建一个字典,其中键是三个字符串的组合?
- oop - oops 消息中的堆栈是什么意思?
- openlayers - 如何在 openlayers 6 中使用 wfst?
- python - 当包含额外内容时,如何使 PIP 仅安装入口点
- php - PHP在本地主机上工作,但不在实时网站上
- javascript - Firestore 更新功能不适用于 collectionGroup
- mendix - 基本 Mendix 应用程序 - ImageViewer 中未显示图像
- mysql - 远程设置和使用mysql服务器的麻烦