89502

Using selenium, I need to iterate over all iframes and their child iframes on the fly

Question:

<strong>I need to iterate over all iframes and their child iframes on the fly as I want to perform some action on all the <img> tags. I am using selenium.</strong>

public class logic { static WebDriver driver; static List<WebElement> TotaliinnerFrames=null; logic() { driver=new FirefoxDriver(); driver.get("aaaa.html"); } public static void main(String[] args) { logic log=new logic(); find_parent_iFrame(); } private static void find_parent_iFrame() { List<WebElement> TotaliFrames = driver.findElements(By.tagName("iframe")); System.out.println("TotaliFrames\t"+TotaliFrames.size()); for(WebElement frame:TotaliFrames) { System.out.println("calling main frame==============================>\t"); findLandingPage(frame); } if(TotaliFrames.size()>0) { driver.switchTo().defaultContent(); } else { List<WebElement> canvas = driver.findElements(By.tagName("canvas")); List<WebElement> img = driver.findElements(By.tagName("img")); List<WebElement> anchor = driver.findElements(By.tagName("a")); if(canvas.size()>0) { System.out.println("inside canvas"); for(WebElement canvTemp : canvas) { callClick(canvTemp); } } if(img.size()>0) { System.out.println("inside img"); for(WebElement imge : img) { callClick(imge); } } if (anchor.size()>0) { System.out.println("inside anchor"); for(WebElement anchorc : anchor) { callClick(anchorc); } } } } private static void findLandingPage(WebElement frame) { driver.switchTo().frame(frame); List<WebElement> canvas = driver.findElements(By.tagName("canvas")); List<WebElement> img = driver.findElements(By.tagName("img")); List<WebElement> anchor = driver.findElements(By.tagName("a")); System.out.println("canvas\t"+canvas.size()); System.out.println("img\t"+img.size()); System.out.println("anchor\t"+anchor.size()); if(canvas.size()>0) { System.out.println("inside canvas"); for(WebElement canvTemp : canvas) { callClick(canvTemp); } } if(img.size()>0) { System.out.println("inside img"); for(WebElement imge : img) { callClick(imge); } } if(anchor.size()>0) { System.out.println("inside anchor"); for(WebElement anchorc : anchor) { callClick(anchorc); } } TotaliinnerFrames = driver.findElements(By.tagName("iframe")); System.out.println("TotaliinnerFrames\t"+TotaliinnerFrames.size()); int innerframesize=TotaliinnerFrames.size(); if(innerframesize>0) { System.out.println("TotaliinnerFrames\t"+innerframesize); for(WebElement inframe:TotaliinnerFrames) { System.out.println("calling inner recursive\t"); findLandingPage(inframe); } TotaliinnerFrames.clear(); } else { driver.switchTo().defaultContent(); } } private static void callClick(WebElement canvTemp) { try { canvTemp.click(); } catch(Exception e) { System.out.println("Exception unable to click"); } }

}

<h2> <strong>I need to Iterate over all iframe and its child iframe on the fly i want all tags and perform some action. using selenium.</strong></h2>

Answer1:

You can't hold references to WebElements across page loads (e.g. initiated by clicks), as they are only valid on their <strong>own</strong> page. They're not just any old Java object. You will get StaleElementReferenceException as you've seen.

Work page by page, not <em>across</em> multiple pages.

<strong>Edit:</strong> here's <a href="https://stackoverflow.com/a/16244739/954442" rel="nofollow">an even better explanation</a>.

Recommend

  • I am writing a Selenium script in Firefox but I am getting “Untrusted Certificate”
  • Running multiple classes in TestNG
  • video tag html5 with src from base64_encode not work on different device and browser
  • How to resume embedded YouTube video?
  • Resize of Iframe dynamically depending on the content of the Iframe of the Iframe's src URL
  • CKEditor and iFrame/YouTube/Other Embed Plugins Show Weird HTML Code
  • Print an iFrame using document.frames in Firefox
  • Protractor testing in iframe in other non-Angular application?
  • How to navigate between different html pages in Windows 8 Metro application using javascript?
  • SQL duplicate rows with multiple left joins
  • Botframework - resize bot chat in width [duplicate]
  • ngFor youtube links with Domsanitizer in Angular2
  • Javascript Array, Object, Date not defined
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Display java JPanel in a JFrame
  • Detect when Facebook like button is clicked
  • How to Cache Real-time Data?
  • Spring Data JPA custom method causing PropertyReferenceException
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to get icons for entities from eclipse?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How to set the response of a form post action to a iframe source?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?