2

I am using Selenium webdriver (Java) and ran into a problem.

I made the method,

    public void escape() {
        Actions action = new Actions(driver);
        action.sendKeys(Keys.ESCAPE).build().perform();
    }

but it does not work for Safari. It works for all other browsers (IE, Firefox, Chrome) except Safari. What will be the alternate escape method for Safari using selenium?

3

It looks like the error is in your code.

action.sendKeys(Keys.ESCAPE).build().perform();

perform is meant to be called as a method of sendKeys, not as a method of build.

This answer on SO has an example:

action.sendKeys(Keys.ESCAPE).perform();
|improve this answer|||||
  • Still does not work for Safari. It works for all other browsers except Safari. – Robben Nov 17 '15 at 16:11
  • thanks , this is working like a charm for me! – eugene.polschikov Apr 10 '19 at 16:49
1

You can use the below code for escape key:

driver.findElement(By.xpath("myobject")).sendKeys(Keys.ESCAPE);

to use this code, you need to add the below mentioned import:

import org.openqa.selenium.Keys;

Another way is using the robot keys, though i never prefer them, below is the code if it helps you:

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_ESCAPE);
|improve this answer|||||
1

If you are using Java you can go with Robot Library.

 Robot r = new Robot();

 r.keyPress(KeyEvent.VK_ESCAPE);

 r.keyRelease(KeyEvent.VK_ESCAPE);
|improve this answer|||||
1

You can try using robot class of java, following is the code.

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.