1

iframe id=pngNewCase1143241142570_IFrame., this id changes dynamically every time how can i handle this in selenium webdriver java I tried using

driver.switchTo().frame(driver.findElement(By.xpath("Xpath of Iframe which changes dynamically")));
driver.switchTo().defaultContent();

but didn't work for me

also tried

driver.switchTo().frame(driver.findElement(By.Id("pngNewCase1143241142570_IFrame")));
driver.switchTo().defaultContent();

can anyone help me please

Muath
  • 4,351
  • 12
  • 42
  • 69
user3567499
  • 11
  • 1
  • 1
  • 2
  • You can switch by frame index. Identify the number of frames and provide the frame index. like driver.switchTo().frame(0) Refer [link](http://stackoverflow.com/questions/20069737/how-to-identify-and-switch-to-the-frame-in-selenium-webdriver-when-frame-does-no) – Ashish Apr 24 '14 at 07:42

4 Answers4

3

If your iframe has a src attribute try to mention it in ur xpath declaration. Its working for me. Try like below

WebElement frame=driver.findElement(By.xpath("//iframe[@src='showNewClaimForm.action']");
driver.switchTo().frame(frame);
demongolem
  • 9,474
  • 36
  • 90
  • 105
Param
  • 31
  • 3
0

You Should Consider using Xpath with ID Contains "//iframe[contains(@id,'frame')]"

Zach
  • 986
  • 7
  • 19
  • but when i log out and log in the id would change to some other number – user3567499 Apr 24 '14 at 09:58
  • Zach is this how u mean to do driver.switchTo().frame(driver.findElement(By.xpath("iframe[contains(@id=pngNewCase1143241142570_IFrame,'frame')]"))); – user3567499 Apr 24 '14 at 10:02
  • It all depends on how many iframes you are dealing with, ideally if its one that has dynamic id i'd use driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id,'IFrame')]"))); Look for a keyword that always exist even when ID is changed i can see IFrame/frame post fixed – Zach Apr 24 '14 at 11:28
  • Thank You Zach...... Zach my drop down in iframe is not working i tried many different methods but couldn't make it work but when i use the same code for my demo project it is working and my demo project is only in html – user3567499 Apr 24 '14 at 13:36
  • Can you provide the Markup of where is not working and how you are doing it? I'd advice to debug step by step and see what is actually going wrong, i.e what elements are coming back negative – Zach Apr 24 '14 at 13:43
0
    int size = driver.findElements(By.tagName("iframe")).size();
    System.out.print("Total Frames --"+size);

    
    for(int i=0; i<=size; i++){
        driver.switchTo().frame(i);
        int total=driver.findElements(By.xpath("//*/th[1]/div/span/a[@id='pui_colmenu']")).size();
        System.out.println(total);
        driver.switchTo().defaultContent();}
0

Fetch the dynamic iframe id into a string and then use the same id to locate the dynamic iframe. Below is for reference:

String iframeId = driver.findElement(By.xpath("*//div[contains(@class,'classname")).getAttribute("id");

        WebElement dynamicIframeId = driver.findElement(By.id(iframeId));
        
        driver.switchTo().frame(dynamicIframeId); 
Rajeev
  • 89
  • 1
  • 1