0
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());*/
}

Selenium-code issue: trying to automate right click but when I right click, it does not move to right-click options instead it clicks on elements. I want to select the option "Open the link on new tab"

David Guyon
  • 2,759
  • 1
  • 28
  • 40
  • 1
    Possible duplicate of [Select an Option from the Right-Click Menu in Selenium Webdriver - Java](https://stackoverflow.com/questions/11428026/select-an-option-from-the-right-click-menu-in-selenium-webdriver-java) – Balderk May 20 '19 at 15:25

1 Answers1

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

// here "t" performs new tab operation. If you use Keys.ARROW_DOWN of actions class will // not move mouse on to the context menu and also, we can't inspect options available on // context menu

}
Sreenivasulu
  • 494
  • 2
  • 7
  • It does the same thing, it did not switch over to the right click options – littin jomon May 21 '19 at 18:22
  • Here, you have to open the link in new tab or you have to navigate to 'Open link in New Tab' and click on it? If your answer is to open in new tab, pass 't' using sendKeys(). It will do the same operation what you used to manually by right clicking on link and selecting 'Open link in new tab'. – Sreenivasulu May 22 '19 at 12:31
  • If your answer is to navigate on 'Open link in new tab' and click on it, first you have to write xpath for the same which is not allowed here. why because we can't inspect context click options. In your code you are trying to hover on Open link in new tab by passing arrow keys which won't work, why because selenium does not know where mouse is hovered. In those cases, when you use keys down or up arrow will not move your mouse to the expected link – Sreenivasulu May 22 '19 at 12:36