Skip to content
Advertisement

Selenium Webdriver module: Keys.UP does not work as expected

I have written a python script that should automatically play the game 2048 (https://play2048.co/).

The problem is: keystrokes seem to be ignored by the browser. Or the program runs too fast for having the browser clicking through the game. I have checked the Selenium documentation and I am not sure if I have to include some explicit waits.

Here is my code:

JavaScript

The goal of the game is reaching the score 2048 by sliding tiles upon eachother by hitting the arrow keys. The game is over when the square is full and you haven’t reached a 2048-tile.

I am not sure what happens during my while loop. The browser seems to ignore the keystrokes of the arrow keys. The tiles are not moved. Every simulated click on an arrow key should move the tiles.

What might be the problem here?

Code update

JavaScript

Advertisement

Answer

Your click() function doesn’t actually click anything:

JavaScript

Your code here is just referencing Keys.UP object. Instead, you need to find an element and send that key to it:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement