使用 Selenium 将按键输入发送到 HTML5 游戏
Send key input to HTML5 game using Selenium
我正在尝试使用 Selenium 将关键输入发送到我使用 Phaser 创建的 HTML5 游戏。但是,我很困惑为什么我不能让它工作。当我在 2048 或 google、python 等其他网站上尝试时(但在通过移相器构建的其他 HTML5 游戏上无效)时,相同的代码有效。任何提示或指示都会非常有用!
python代码下方:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://dry-anchorage-61733.herokuapp.com/") #this is game link
#driver.get("https://gabrielecirulli.github.io/2048/") #works for 2048
actions = ActionChains(driver)
for _ in range(6):
actions.send_keys(Keys.ARROW_UP).perform()
time.sleep(1)
actions.send_keys(Keys.ARROW_LEFT).perform()
time.sleep(1)
selenium 似乎没有专注于您的应用程序。
尝试使用发送键点击元素和链
element = driver.find_element_by_tag_name("canvas")
actions.click(element).key_down(Keys.ARROW_LEFT).perform()
这对我有用
我正在尝试使用 Selenium 将关键输入发送到我使用 Phaser 创建的 HTML5 游戏。但是,我很困惑为什么我不能让它工作。当我在 2048 或 google、python 等其他网站上尝试时(但在通过移相器构建的其他 HTML5 游戏上无效)时,相同的代码有效。任何提示或指示都会非常有用!
python代码下方:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://dry-anchorage-61733.herokuapp.com/") #this is game link
#driver.get("https://gabrielecirulli.github.io/2048/") #works for 2048
actions = ActionChains(driver)
for _ in range(6):
actions.send_keys(Keys.ARROW_UP).perform()
time.sleep(1)
actions.send_keys(Keys.ARROW_LEFT).perform()
time.sleep(1)
selenium 似乎没有专注于您的应用程序。
尝试使用发送键点击元素和链
element = driver.find_element_by_tag_name("canvas")
actions.click(element).key_down(Keys.ARROW_LEFT).perform()
这对我有用