【问题标题】:python: How can I press arrow keys randomly seleniumpython:如何随机按箭头键硒
【发布时间】:2017-06-22 23:28:20
【问题描述】:

我正在尝试通过随机选择箭头键来制作一个播放2048 的程序。

我试过这样的:

 moves = [htmlElem.send_keys(Keys.UP),htmlElem.send_keys(Keys.RIGHT),htmlElem.send_keys(Keys.DOWN),htmlElem.send_keys(Keys.LEFT)]


while True:
    random.choice(moves)

这不起作用。我试过print(random.choice(moves)),但它无限循环None

那么如何使用 Selenium 随机按下箭头键?

【问题讨论】:

  • 尝试使用from selenium.webdriver.common.keys import Keys element = driver.find_element_by_id("Value") element.send_keys("keysToSend") element.submit()

标签: python-3.x selenium random while-loop automated-tests


【解决方案1】:

这似乎有效。试试看,让我知道结果:

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

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

【讨论】:

  • 它工作我改变moves = [Keys.LEFT, Keys.DOWN, Keys.RIGHT, Keys.UP]htmlElem.send_keys(random.choice(moves))
猜你喜欢
  • 2017-12-26
  • 1970-01-01
  • 2022-07-26
  • 2017-11-27
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
相关资源
最近更新 更多