尝试自动单击鼠标右键,然后选择第一个选项“在新选项卡中打开链接”

时间:2019-05-20 14:53:36

标签: java selenium-webdriver browser-automation

public void openNewEbay() throws InterruptedException {
    // to click on open link on new tab
    Actions rightclick = new Actions(driver);
    WebElement elementlocator = driver
            .findElement(By.xpath("//li[@class='hl-cat-nav__js-tab']//a[contains(text(),'Electronics')]"));

    //rightclick.contextClick(elementlocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
    Thread.sleep(3000);
    rightclick.contextClick(elementlocator).build().perform();
    Thread.sleep(5000);
    rightclick.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();

    rightclick.sendKeys(Keys.RETURN).build().perform();

    /*ArrayList tab = new ArrayList(driver.getWindowHandles());
    System.out.println(tab.size());*/
}

public void openNewEbay() throws InterruptedException {
    // to click on open link on new tab
    Actions rightclick = new Actions(driver);
    WebElement elementlocator = driver
            .findElement(By.xpath("//li[@class='hl-cat-nav__js-tab']//a[contains(text(),'Electronics')]"));

    //rightclick.contextClick(elementlocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
    Thread.sleep(3000);
    rightclick.contextClick(elementlocator).build().perform();
    Thread.sleep(5000);
    rightclick.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();

    rightclick.sendKeys(Keys.RETURN).build().perform();

    /*ArrayList tab = new ArrayList(driver.getWindowHandles());
    System.out.println(tab.size());*/
}

硒代码问题:尝试自动执行右键单击,但是当我右键单击时,它不会移动到右键单击选项,而是单击元素。我要选择“在新标签页上打开链接”选项

1 个答案:

答案 0 :(得分:0)

Try below code:
public void openNewEbay() throws InterruptedException {
    // to click on open link on new tab
    Actions rightclick = new Actions(driver);
    WebElement elementlocator = driver
            .findElement(By.xpath("//li[@class='hl-cat-nav__js-tab']//a[contains(text(),'Electronics')]"));

    //rightclick.contextClick(elementlocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
    Thread.sleep(3000);
    rightclick.contextClick(elementlocator).build().perform();
    Thread.sleep(5000);
    rightclick.sendKeys("t").build().perform();

//这里“ t”执行新的制表符操作。如果使用actions类的Keys.ARROW_DOWN将 //不要将鼠标移到上下文菜单上,而且,我们无法检查//上下文菜单上可用的选项

}