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(); } }
Advertisement
Answer
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));
}