Selenium ChromeDriver Action and SendKeys don´t press the button

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

1 answer

  • answered 2021-03-07 19:28 Greg Burghardt

    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.