Click a link within an iFrame which opens a new tab and switch to it

0 votes

I am working on a code which includes clicking on a link and that should open in a new tab using webdriver, the problem is.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]); }

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>

It simply opens the required page in same tab.

Jun 7, 2018 in Selenium by Martin
• 4,220 points
648 views

1 answer to this question.

0 votes

While switching the TAB induce WebDriverWait and switch accordingly. Below code will help you:

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

        }

    }

}
answered Jun 7, 2018 by Samarpit
• 5,130 points

Related Questions In Selenium

0 votes
1 answer
0 votes
5 answers
0 votes
2 answers

How to open a link in a new tab?

try this :- driver.findElement(By.linkText("Business")).sendKeys(Ke ...READ MORE

answered Jan 10 in Selenium by kaloo
220 views
0 votes
1 answer

How to open new tab in same browser and switch between them using Selenium?

Hi Mugdha, you can use following code ...READ MORE

answered May 24 in Selenium by Abha
• 27,180 points
1,097 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
1,019 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,160 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
897 views
0 votes
1 answer

Click on a button within a pop-up window with python selenium

It's not an Alert but a Modal Dialog Box. You ...READ MORE

answered Jun 20, 2018 in Selenium by Samarpit
• 5,130 points
8,244 views
0 votes
2 answers