0

I've been looking for an answer to this, but all of the topics about it was really outdated.

The situation is, I have a flash game on the browser that I'm trying to play programmatically using the arrow keys. I know how to send a signal to the keys though, ex:

driver.FindElement(By.Id("myflash")).SendKeys(Keys.ArrowLeft);

But can't really get past that part.

Senpoo
  • 1
  • 4
  • Do you want to hold the arrow key or do you rather need an additional ("modifier") key? In the latter case probably this is all you need https://stackoverflow.com/questions/37831787/how-to-press-down-arrow-key-followed-by-enter-button-in-selenium-webdriver – Markus Deibel Apr 09 '19 at 07:03
  • As in, hold the arrow key for 5 seconds. – Senpoo Apr 09 '19 at 07:11

2 Answers2

0

Can you try using chords, as in

driver.FindElement(By.Id("myflash")).sendKeys(Keys.chord(Keys.ARROW_LEFT));
  • Well, I tried that solution myself before suggesting it. I am not sure why it didn't work for you. It just came to me that I had also fired a click event on that particular web element before trying to use the event for ARROW_LEFT. May I request you to try that as well? e.g. `driver.FindElement(By.Id("myflash")).click();` and then firing `driver.FindElement(By.Id("myflash")).sendKeys(Keys.chord(Keys.ARROW_LEFT));` – Jasvinder Singh Chugh Apr 09 '19 at 13:46
0

Try using the Actions.

Try this

new Actions(driver).KeyDown(driver.FindElement(By.Id("myflash")), Keys.ArrowLeft).perform();
S Ahmed
  • 1,454
  • 1
  • 8
  • 14
  • @S Ahmed Thanks for your answer, I learned how to add actions now, something I didn't know before. But for your answer it gives me an error message saying: "System.ArgumentException: 'key must be a modifier key (Keys.Shift, Keys.Control, Keys.Alt, Keys.Meta, Keys.Command, Keys.LeftAlt, Keys.LeftControl, Keys.LeftShift) Parameter name: key'" – Senpoo Apr 09 '19 at 12:17
  • @Senpoo Since nonmodifier keydown is not acceptable in actions, you should try this solution https://stackoverflow.com/questions/36266151/how-to-press-and-hold-non-modifier-key-space-key-using-selenium – S Ahmed Apr 09 '19 at 13:28