简体   繁体   中英

C# and Selenium; Possible to press Windows key?

I would like to move my browser window to the left or right side of the screen. Manually I would press[WINDOW] + [LeftArrow] (or [RightArrow]) to achieve this.

I've read this one but it's for Java (see comments) but I can't find a way to press the windows key when using C#.

My best option seems to be:

driver.Manage().Window.Position = new Point(x-coord, y-coord);
driver.Manage().Window.Size = new Size(myWidth,myHeight); 

but seems to be a bit fiddly, and I'm sure there is a better way.

I have also tried:

Actions action = new Actions(driver);
action.SendKeys(Keys.   )

Then Keys doesn't have the windows key...

If you'd like to go for they SendKeys approach, sending key combinations that include the Windows logo key is not straightforward. The equivalent for the Windows logo key alone would be Strg + Esc . While that can be simulated, Strg + Esc + Left would just open the start menu (or whatever they call it now in Windows 10), and then send another left arrow.

But here there is a solution.

dllatikay 's answer is very helpful for understanding how things work. I will just add a working example - how to maximize browser window when using Selenium:

 // Install-Package InputSimulator in package manager
 var inputSim = new InputSimulator();
 // WinKey + Up maximizes current focused window
 inputSim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.UP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM