Not able to retract a link out of iFrame in TOI website | Selenium Forum
M
Posted on 13/09/2015
Hi Ashish/All,

I am trying to extract link from one of Iframe based on Tutorial 15 but not able to get it. Any help will be much appreciated.

Code :

package qtpselenium.tutorial15;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Iframe_basic {

public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\resources\\"+"chromedriver.exe");
driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,10);
driver.navigate().to("http://timesofindia.indiatimes.com/");
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("html/body/table/tbody/tr[1]/td/table/tbody/tr/td[2]/a")));
List<WebElement> iFrames =driver.findElements(By.tagName("iframe"));


String linkToBeClicked = "html/body/div[1]/ul/li[1]/h3/a";
int i=0;

for(WebElement w : iFrames){
driver.switchTo().frame(w);
int size = driver.findElements(By.xpath(linkToBeClicked)).size();
if(size>0){
System.out.println("Element is present in "+i+"th frames");
}

driver.switchTo().defaultContent();
i++;


}


driver.quit();

}

}

M
Replied on 15/09/2015

your code is a little unclear. could you please send a screen shot which link do you want to retract.


M
Replied on 15/09/2015

Hello,

I have attached the screenshot as asked by you, Couple of clarification also :
1.) Any link present in horoscope section i would be interested to fetch in. Since TOI web is subjeted to change so please take any link inside horoscope section and access.
2.) I know how to take care of Nested Iframes But what if we need to traverse through nth iFrames : Looking for some generic function rather utility function which will simply take element to be searched and List of Iframes and also take care of nested iFrames in list. Your help would be appreciated in this scenario.

Please let me know if i am unclear or not able to explain

Responsive image

M
Replied on 19/09/2015

1) I am working on TOI.
2) It is explained in module 15. so what is the trouble you are getting.


M
Replied on 20/09/2015

1.) Thanks, Please let me know once its done. BTW i found some workaround solution which is working fine.
If i am moving to respective element using Actions.moveToElement() and then try to locate element inside frames. it works.

2.) Yes in module 15 explanation is there. But what i a a mean is can we create some generic/utility function which takes
top frame and locator as input and return Element irrespective of any ladder of nesting in frames at any level.
I assume there should be recursive function need to be written for this.


M
Replied on 21/09/2015

1.) glad to know it.
2.) its not easy to make some thing like that. It would certainly be much easier to different function for different webpage.