0

I am working on a project which includes clicking on a link and that should open in a new tab using webdriver, the problem is

  1. The supposed link is contained in iFrame, soshift+click isn't working

    private void openInNewTabAndSwitch(WebElement linkElement) {
    // logic of opening in new tab goes here...
    Actions newTab = new Actions(driver);
    newTab.keyDown(Keys.SHIFT).click(linkElement).keyUp(Keys.SHIFT).build().perform();
    Set<String> windowSet = driver.getWindowHandles();
    driver.switchTo().window((String) windowSet.toArray()[1]); }
    
  2. I cannot find the href attribute since some javascript function is opening it using some onClick()

<a onclick="javascript:LinkOccam (this, 'opportunity');">Mednomics Proposition</a>

Problem:- It simply opens the required page in same tab.

Now, I cannot find anything related to this, please help..!

Other related info I am using windows 7, Java 8, ChromeDriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Salim Shamim
  • 656
  • 10
  • 25

1 Answers1

0

While you click on a WebElement which opens a New TAB you don't need to worry about whether the WebElement is on Top-Level Browsing Context or within an iFrame. Additionally while switching the TAB induce WebDriverWait and switch accordingly. To achieve that you can use the following code block :

private void openInNewTabAndSwitch(WebElement linkElement) 
{
    String parentTab = driver.getWindowHandle();
    Actions newTab = new Actions(driver);
    newTab.keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
    WebDriverWait wait = new WebDriverWait(driver,5);
    wait.until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> windowSet = driver.getWindowHandles();
    for(String tab:windowSet)
    {
        if(!parentTab.equalsIgnoreCase(tab))
        {
            driver.switchTo().window(tab);
            //do your work in the newly opened TAB
        }
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352