【问题标题】:How to right click on a link and open the link in a new tab using Selenium through Java如何通过 Java 使用 Selenium 右键单击​​链接并在新选项卡中打开链接
【发布时间】:2020-09-20 17:09:43
【问题描述】:

我正在尝试使用 Selenium 右键单击​​ Facebook 登录页面上的 Forgotten account? 链接,但它不起作用。

我正在尝试在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();

}
}

【问题讨论】:

  • `动作动作=新动作(驱动程序); action.keyDown(Keys.LEFT_CONTROL).moveToElement(link).click().keyUp(Keys.LEFT_CONTROL).build().perform();` 修改代码这会在新选项卡中打开链接,但选项卡会立即关闭并且链接会在主选项卡中打开。
  • 您需要编辑您的问题以包含所有信息。评论不适合这个。

标签: java selenium selenium-webdriver tabs webdriverwait


【解决方案1】:
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));

}

【讨论】:

    【解决方案2】:

    您可以按 ctrlclick() 在新标签页中打开链接,而不是右键单击链接并在新标签页中打开链接使用以下Locator Strategies 切换到新标签:

    • 代码块:

      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");
                  }
              }
          }
      }
      
    • 控制台输出:

      The mainwindow handle is CDwindow-0753C465F9132427837081CE5AB8C67D
      The childwindow id is CDwindow-79C688CE476CA8EC4729EFFDE93C84EA
      
    • 浏览器快照:


    参考

    您可以在以下位置找到一些相关的详细讨论:

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2021-12-21
      相关资源
      最近更新 更多