1

I am trying to right click on the Forgotten account? link on the Facebook login page using Selenium but it is not working.

I am trying to send.Keys() after contextClick() but the key press is happening on the page and not on the context menu.

package keyboardandmouseaction;

import java.awt.AWTException;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class testcase8 {
    public static void main(String[] args) throws AWTException, InterruptedException {

        System.out.println("Running keyboardandmouseactions > testcase8");

        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");

        WebElement link=driver.findElement(By.xpath("//a[contains(text(),\"Forgotten account?\")]"));
        Actions a=new Actions(driver);

        // defective code start
        Action builder=a.moveToElement(link).contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build();
        // defective code end
        builder.perform();



        Set<String> windowid =driver.getWindowHandles();
        Iterator<String> itr =windowid.iterator();

        String mainwindow=itr.next();
        String childwindow=itr.next();
        System.out.println("The mainwindow id is "+mainwindow);
        System.out.println("The childwindow id is "+childwindow);
        driver.switchTo().window(childwindow);
        driver.get("http://demo.automationtesting.in/Alerts.html");
        driver.close();

}
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
SBRoy
  • 13
  • 1
  • 4
  • ` Actions action = new Actions(driver); action.keyDown(Keys.LEFT_CONTROL).moveToElement(link).click().keyUp(Keys.LEFT_CONTROL).build().perform();` modifying the code this opens the link in a new tab but the tab closes instantly and the link is opened in the main tab. – SBRoy Jun 02 '20 at 11:54
  • 1
    You need to edit your Question to include all information. Comments are not suited for this. – Scratte Jun 03 '20 at 06:27

3 Answers3

1

Instead of right click on the link and open the link in a new tab you can press ctrl and click() to open the link in a new tab and finally switch to the new tab using the following Locator Strategies:

  • Code Block:

    import java.util.Collections;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Control_Click {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.facebook.com/");
            WebElement forgotPassword = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Forgotten account?")));
            String parentWindow = driver.getWindowHandle();
            System.out.println("The mainwindow handle is "+driver.getWindowHandle());
            new Actions(driver).keyDown(Keys.CONTROL).click(forgotPassword).keyUp(Keys.CONTROL).build().perform();
            new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
            for(String window:driver.getWindowHandles()) {
                if(!parentWindow.equalsIgnoreCase(window)) {
                    driver.switchTo().window(window);
                    System.out.println("The childwindow id is "+driver.getWindowHandle());
                    driver.get("http://demo.automationtesting.in/Alerts.html");
                }
            }
        }
    }
    
  • Console Output:

    The mainwindow handle is CDwindow-0753C465F9132427837081CE5AB8C67D
    The childwindow id is CDwindow-79C688CE476CA8EC4729EFFDE93C84EA
    
  • Browser Snapshot:

ctrl_click


Reference

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement link=driver.findElement(By.xpath("//a[contains(text(),\"Forgotten account?\")]"));
Actions actions = new Actions(driver);

actions.keyDown(Keys.LEFT_CONTROL)
        .click(element)
        .keyUp(Keys.LEFT_CONTROL)
        .build()
        .perform();

ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));

}

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
0

This is what worked for me using Java - Selenium and not passing a target URL using MAC

WebElement element = find(By.cssSelector ("yourLocator")); 
Actions actions = new Actions(getDriver()); 
actions.
    moveToElement(element)
    .keyDown(Keys.COMMAND)
    .click()
    .keyUp(Keys.COMMAND)
    .build()
    .perform();

new WebDriverWait(getDriver(),10).until(ExpectedConditions.numberOfWindowsToBe(2)); 
ArrayList<String> tabs = new ArrayList<String>(getDriver().getWindowHandles()); 
getDriver().switchTo().window(tabs.get(1));