python:如何随机按箭头键硒

Fre*_*ddy 6 random selenium automated-tests while-loop python-3.x

我正在尝试通过随机选择箭头键来制作一个播放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)
Run Code Online (Sandbox Code Playgroud)

这是行不通的。我试过了print(random.choice(moves)),但它无限循环None

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

And*_*son 9

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

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))
Run Code Online (Sandbox Code Playgroud)