python:如何随机按箭头键硒 [英] python: How can I press arrow keys randomly selenium

查看:57
本文介绍了python:如何随机按箭头键硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过随机选择箭头键来制作一个播放 2048 的程序.

I am trying to make a program which plays 2048 by randomly choosing arrow keys.

我尝试过这样的事情:

 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

This is not working. I tried print(random.choice(moves)), but it infinite loop of None

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

So how can I press arrows keys randomly using Selenium?

推荐答案

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

This seem to work. Try it and let me know the result:

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))

这篇关于python:如何随机按箭头键硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆