HTMLHi I am working on automation -selenium with java. I need to click on a button which is inside a modal popup and the whole is inside an iframe. I tried the following code:


I get the element with class "modal-content" in the pagesource but NoSuchElement Exception when i run the automation script. Is there any possible way to find the element? Thanks.

  • Could you possibly add the relevant HTML? I have the suspicion that your modal might live within another sandbox (frame, iframe, window...) but without seeing the markup I can't really help you. – Daniel Jun 4 '19 at 9:47
  • @Daniel I have added the screenshot of the html! – Sina Jun 4 '19 at 10:03
  • Hi @Sina. Adding the actual code would be much better than a screenshot--code is much more reasonable and answerers could actually try the code when suggesting solutions, which could help you get better answers. – c32hedge Jun 4 '19 at 13:24

Try this:-


Here i'm switching the frame using ID.

| improve this answer | |
  • The frame id is generated dynamically only the first part- "frame-preview" is static the rest is dynamically generated. – Sina Jun 4 '19 at 10:33
  • ok then use, as you was using it driver.switchTo().frame(2); – Upkar Singh Jun 4 '19 at 10:33
  • Still unable to locate element error. – Sina Jun 4 '19 at 10:48
  • Is your frame number is correct?, i mean frame size – Upkar Singh Jun 4 '19 at 10:59
  • 1
    I tried this and it works..!WebElement iframe = driver.findElement(By.xpath("//iframe[starts-with(@id,'frame-preview-')]")); driver.switchTo().frame(iframe); driver.findElement(By.xpath(".//span[contains(text(),'Frozen')]")).click(); – Sina Jun 4 '19 at 11:25

Okay, the Problem you're facing are your js sandboxes. An iframe loads the entire content of another page into your parent page, including a separate execution environment for Javascript. To access this environment you need to explivitly call driver.switchTo().frame(WEBELEMENT) before you can interact with the elements.

In your case driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id*='frame-preview']")); followed by your element query should work.

If it doesn't try going up your tree and look if you're possibly within a nested iframe. In that case you need to switch step by step from the most outer to your target frame.

| improve this answer | |
  • Could you please elaborate on how to switch from step by step. – Sina Jun 4 '19 at 10:49
  • Thank you for the responses! – Sina Jun 4 '19 at 11:28

If it is the only iframe starting with frame-preview locator you can use partial css locator (or xpath - up to you) to explicity state the frame you want to change to:

List<WebElement> iframes = driver.findElements(By.cssSelector("iframe[id^='frame-preview']");

Then try some more explicit selector for the modal-content:

WebElement modalContent = driver.findElement(By.cssSelector("div[data-rec-id='FloatingPanelComponent-modalContainer] div[class='modal-content']");

If this results in NoSuchElementException you should probably introduce some timeout. Test it by placing a breakpoint at the WebElement modalContent line. If waiting resolves your issue, introduce an ExplicitWait, like this:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[data-rec-id='FloatingPanelComponent-modalContainer] div[class='modal-content']")));
| improve this answer | |
  • There are 2 iframes with id starting with "frame-preview"! – Sina Jun 4 '19 at 11:05
  • Then capture a List of iframes, and use the one that you need. I will edit my answer. – Moro Jun 4 '19 at 11:10
  • Thank you for the responses! – Sina Jun 4 '19 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.