简体   繁体   English

如何右键单击链接并使用 Selenium 至 Java 在新选项卡中打开链接

[英]How to right click on a link and open the link in a new tab using Selenium through Java

I am trying to right click on the Forgotten account?我正在尝试右键单击“被遗忘的帐户”? link on the Facebook login page using Selenium but it is not working.使用 Selenium 在 Facebook 登录页面上链接,但它不起作用。

I am trying to send.Keys() after contextClick() but the key press is happening on the page and not on the context menu.我正在尝试在contextClick() send.Keys() ) 但按键发生在页面上而不是上下文菜单上。

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();

}
}

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 :无需右键单击链接并在新选项卡中打开链接,您可以按ctrlclick()在新选项卡中打开链接,最后使用以下定位器策略切换到新选项卡:

  • 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:控制台 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:您可以在以下位置找到一些相关的详细讨论:

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));

} }

This is what worked for me using Java - Selenium and not passing a target URL using MAC这就是我使用 Java - Selenium 而不是使用 MAC 传递目标 URL 的方法

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何单击新标签中的打开链接以使用 selenium webdriver 在主标签中显示的新标签中打开链接 - How to click on open link in new tab to open the link in new tab that appears in main tab using selenium webdriver 如何使用 Selenium WebDriver 在新标签页(chrome)中打开链接? - How to open a link in new tab (chrome) using Selenium WebDriver? 在Chrome中打开新标签页(不在新标签页中链接),并专注于此 - open new tab(not link in new tab) in Chrome and focus on it Selenium java 如何在Java中使用Selenium在菜单中的新选项卡中打开一个链接 - How to open one by one link in new Tab from Menu using Selenium in Java 如何在Selenium 3中打开新选项卡中的链接并关闭硒鼓中的选项卡? - How to Open link in new tab and close tab in selenium 3? 无法单击上下文菜单 - 在 selenium Java 中的新 window 中打开链接 - Not able to Click on context Menu - Open link in new window in selenium Java Selenium 2:在新标签页中打开链接并关闭标签页 - Selenium 2: Open link in new tab and close tabs 如何使用Java Selenium单击AngularJS链接? - How to click on an AngularJS link using Java Selenium? 尝试自动右键单击并选择第一个选项“在新选项卡中打开链接” - Trying to automate a right-click and choose the first option “Open the link in a new tab” 如何使用 Selenium Java 单击应在同一窗口中打开的链接 - How to click on a link which should be open in same window using Selenium Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM