Home > database >  Not able to Click on context Menu - Open link in new window in selenium Java
Not able to Click on context Menu - Open link in new window in selenium Java

Time:09-16

enter image description here**Right click is working but not able to click on Open link in new window option. **

public static void newwindow() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\TechExtensor\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
        Thread.sleep(3000);
        Actions actions = new Actions(driver);
        WebElement elementLocator = driver.findElement(By.xpath("//*[@id=\"main_navbar\"]/ul/li[3]/a/span"));
        actions.moveToElement(elementLocator);
        Thread.sleep(2000);
actions.contextClick(elementLocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
    }

CodePudding user response:

Use Robot class.

    Actions actions = new Actions(driver);
    WebElement elementLocator = driver.findElement(By.xpath("//*[@id=\"main_navbar\"]/ul/li[3]/a/span"));
    actions.contextClick(elementLocator).perform();
    Thread.sleep(1000);

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyPress(KeyEvent.VK_ENTER);
  • Related