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,330 points
1,676 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,890 points

Related Questions In Selenium

+1 vote
1 answer
0 votes
7 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

Try this once driver.findElement(By.id("id")).sendKeys(Keys.CONT ...READ MORE

answered Sep 5 in Selenium by Sri
• 2,970 points
45,591 views
0 votes
2 answers

How to open a link in a new tab?

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

answered Jan 10, 2019 in Selenium by kaloo
373 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, 2019 in Selenium by Abha
• 27,930 points
8,977 views
0 votes
2 answers

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,510 points
3,470 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,483 views
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,890 points
2,562 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
2,798 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,890 points
16,480 views
0 votes
2 answers