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?

4 Answers 4

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();
2
  • Still does not work for Safari. It works for all other browsers except Safari.
    – Robben
    Nov 17, 2015 at 16:11
  • thanks , this is working like a charm for me! Apr 10, 2019 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);
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);
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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