I am trying to make a program which plays 2048 by randomly choosing arrow keys.
I tried something like this:
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)
This is not working. I tried print(random.choice(moves))
, but it infinite loop of None
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With