0

I have a problem with my SeleniumDriver and it drives me crazy. I almost tried every solution on stackoverflow..

My simple goal is to send a key to the browser, but not to an element. My key is "mediaTrackNext" and can be found at

Windows.Forms.Keys.MediaNextTrack

I tried almost any solution:

Dim actions As Actions = New Actions(TestBot)
actions.SendKeys(Windows.Forms.Keys.MediaNextTrack).Perform()

or

TestBot.FindElement(By.XPath("/html/body")).SendKeys(Windows.Forms.Keys.MediaNextTrack)

or

Dim actions As Actions = New Actions(TestBot)
actions.SendKeys(TestBot.FindElement(By.XPath("//body")), Keys.F12).Perform()

I can´t even send keys like F12 to open the developers console, nothing happens.. F5 doesn´t work as well. And yes, I tried a few Tags and xpaths, but no tag seem to work.

Is there another way? I am so stuck right now..

Thank you. Best regards, xored

xored
  • 1
  • 1
  • Does this answer your question? [How do you automatically open the Chrome Devtools tab within Selenium (C#)?](https://stackoverflow.com/questions/37683576/how-do-you-automatically-open-the-chrome-devtools-tab-within-selenium-c) – Greg Burghardt Mar 07 '21 at 16:53
  • No, because the devtools were only a test if I can let the driver press any keys. My goal is the MediaNextTrack key, but it doesn´t work. – xored Mar 07 '21 at 17:07
  • Hm. That's interesting. I wonder if you can create a test web page with keydown, keypress and keyup event listeners - which then log the event to the browser console - will help determine if this is possible. I wonder which key code it is. – Greg Burghardt Mar 07 '21 at 19:08
  • You cannot send a key to the browser. It must be sent to an element. I would think sending it to the body tag should work. – Greg Burghardt Mar 07 '21 at 19:10
  • Sending it to the body tag does not work, because nothing happens. I will code a chrome extension.. – xored Mar 08 '21 at 16:00

1 Answers1

0

My VB.NET is a tad rusty, so I might have mingled some C# with VB.NET.


The Windows.Forms.Keys is an enum. The integer backing value is a character code. You might need to cast the enum to an int, and then convert it to a char:

Dim charCode As Integer = DirectCast(Windows.Forms.Keys, Integer);
Dim nextTrack As String = Convert.ToChar(charCode).ToString();

TestBot.FindElement(By.XPath("//body")).SendKeys(nextTrack);

If that still does not work, you might be encountering a race condition between Selenium and a JavaScript event handler. Maybe JavaScript hasn't started listening for the key press yet when Selenium sends the keys.

You might need to use an explicit wait for a certain element to appear on screen before sending the "next track" shortcut. Without seeing the HTML for the web page, I would have no idea what to recommend.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92