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,320 points
4,565 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,910 points

Related Questions In Selenium

+1 vote
1 answer
0 votes
8 answers

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

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,890 points
102,739 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 11, 2019 in Selenium by kaloo
1,409 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
• 28,140 points
22,239 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,500 points
13,434 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,006 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,910 points
7,119 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 3, 2020 in Selenium by Sri
• 3,190 points
7,804 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,910 points
31,502 views
0 votes
5 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP