0

I'm trying to find iFrame Web Element but i'm getting "No Such Element Excecption".

This is the way that i'm trying to locate the iFrame:

@FindBy(id="iframe_uz04pghfaa")
public WebElement ifrmContactIframe;

public void SwitchToIframe() throws ParserConfigurationException, SAXException, IOException 
{       
    try 
    {
        ifrmContactIframe.isDisplayed();    //if the element is displayed it means that he exist
        driver.switchTo().frame(ifrmContactIframe);
    }
    catch (Exception e)
    {
        fail("Element does not exist"); 
    }
}

HTML Snapshot: This is picture of the site code

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Lior p
  • 45
  • 6
  • Welcome to Stack Overflow! Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Dec 27 '18 at 23:16
  • Have you checked to see if the ID changes on each refresh? `.isDisplayed()` checks more than just if the element exists... it checks to see if it's visible. You shouldn't need that check because if it doesn't exist, the next line will fail anyway. – JeffC Dec 27 '18 at 23:18

2 Answers2

0

To locate and switch to the iFrame you need to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • You can use the following solution:

    • Using CSS_SELECTOR:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='iframe_'][name^='iframe_'][src^='/crm/contact/details/']")));
      
    • Using XPATH:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id,'iframe_') and starts-with(@name,'iframe_')][starts-with(@src, '/crm/contact/details/')]")));
      

PS: Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @Liorp _FrameId_ and _FrameName_ are dynamically generated. _implicitlyWait_ won't be enough. You need _WebDriverWait_. – undetected Selenium Dec 28 '18 at 05:50
  • I tried the Wait option but it still doesn't recognize the iFrame: ' new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframe_uz04pghfaa")));' – Lior p Dec 28 '18 at 08:56
  • @Liorp Where did you find **By.id("iframe_uz04pghfaa")** within this answer? – undetected Selenium Dec 28 '18 at 08:57
  • I thought that i saw it in one of the answers but never mind, i changed it to the 'By.name' but it still doesn't find the iFrame: 'new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("iframe_uz04pghfaa"))); ' By the way what's the difference between searching by name or by id? – Lior p Dec 28 '18 at 09:10
  • @Liorp Where did you even find **By.name("iframe_uz04pghfaa")** within this answer? – undetected Selenium Dec 28 '18 at 09:11
  • In one of your previous comments about "Ways to deal with #document under iframe" – Lior p Dec 28 '18 at 09:16
0

Most likely your frame is being generated randomly,In Your method, Try Following:

    try 
        {
            List<WebElement> totalFrames = driver.findElements(By.cssSelector("*[id^='iframe_'"));
            System.out.println("Total FRMAES =" + totalFrames .size()); 
            ifrmContactIframe = totalFrames.get(0);

            ifrmContactIframe.isDisplayed();    //if the element is displayed it means that he exist
            driver.switchTo().frame(ifrmContactIframe);
        }
        catch (Exception e)
        {
            fail("Element does not exist"); 
        }
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46