Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to handle platform specific keys in Selenium/Webdriver?

My problem is simple:

I want to do a "select all". This is done differently in macosx compared to linux and windows.

Keys.chord(Keys.COMMAND, "a")

vs

Keys.chord(Keys.CONTROL, "a")

like image 237
l3dx Avatar asked Apr 10 '12 13:04

l3dx


People also ask

Which Selenium class allows you to specify specific keys to press?

1. sendKeys. Using the Actions class in Selenium, we can implement the sendKeys() method to type specific values in the application.

How many ways we can send keys in Selenium?

Now, as we discussed, Selenium WebDriver provides two ways to send any keyboard event to a web element: sendKeys() method of WebElement class. Actions class.

What is the Selenium WebDriver command to use following combination of keys on the keyboard control a?

We can also perform a CTRL+A press by simply using the sendKeys() method. We have to pass Keys. CONTROL along with the string A by concatenating with +, as an argument to the method.


2 Answers

In Java, I do little workaround for this:

String os = System.getProperty("os.name");
if (os.equals("WINDOWS")){
   Keys.chord(Keys.CONTROL, "a");
}else{
   Keys.chord(Keys.COMMAND, "a");
}

Basically - I get the OS where do I run and behave by that accordingly

like image 59
Pavel Janicek Avatar answered Oct 12 '22 12:10

Pavel Janicek


Since Linux and Windows both support CONTROL, then the only difference would be MAC (Darwin), so I would rather use:

Python:

import platform

os_base = platform.system()

If os_base == 'Darwin':

    selector.send_keys(Keys.COMMAND, 'a')

else:

    selector.send_keys(Keys.CONTROL, 'a')
like image 28
Carlos Melo Avatar answered Oct 12 '22 11:10

Carlos Melo