Amit Ronen
Amit Ronen

Reputation: 161

WebDriver - How to locate iframe without id

I'm trying to switch to an iframe in order to locate an element, but I'm unable to locate the iframe since it has no id or name

<div id="eyein-modal" style="display: block; position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; bottom: 0px; right: 0px; z-index: 90000000; background-color: rgba(0, 0, 0, 0.6); overflow: auto; opacity: 1;">
<iframe style="display: block; width:90%; height:90%; border: 0px; margin: 2.5% auto; z-index: 90000000; overflow: hidden;" scrolling="no" src="about:blank">
<html>
   <head>
   <body class="">
      <div id="modal">
      <div id="modal-header">
      <div id="header-logo">
      <div id="title-container" class="">
      <a id="view-event" class="button" target="_blank" href="http://www.link.com">view event</a>
      <div id="close-modal" class="close-dark"></div>

close-modal is the element I need eventually

Upvotes: 1

Views: 19765

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193338

As per the html of the iframe element:

<iframe style="display: block; width:90%; height:90%; border: 0px; margin: 2.5% auto; z-index: 90000000; overflow: hidden;" scrolling="no" src="about:blank">

It is pretty much evident that the <iframe> doesn't have either of the following attributes:

  • id
  • name
  • class
  • title
  • specific value of src attribute

In such cases to switch to the desired iframe you can refer to the preceeding(sibling) WebElement and you can use either of the following locator strategies:

  • Java based solutions:

    • Using xpath and following:

      WebElement iframe = driver.findElement(By.xpath("//div[@id='eyein-modal']//following::iframe[1]"));
      driver.switchTo().frame(iframe);
      
    • Using xpath and following-sibling:

      WebElement iframe = driver.findElement(By.xpath("//div[@id='eyein-modal']//following-sibling::iframe[1]"));
      driver.switchTo().frame(iframe);
      
    • Using cssSelector:

      WebElement iframe = driver.findElement(By.cssSelector("div#eyein-modal +iframe"));
      driver.switchTo().frame(iframe);
      
  • Python based solutions:

    • Using xpath and following:

      driver.switch_to.frame(driver.find_element(By.XPATH, "//div[@id='eyein-modal']//following::iframe[1]"))
      
    • Using xpath and following-sibling:

      driver.switch_to.frame(driver.find_element(By.XPATH, "//div[@id='eyein-modal']//following-sibling::iframe[1]"))
      
    • Using cssSelector:

      driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "div#eyein-modal +iframe"))
      

Reference

You can find a couple of relevant discussions in:

Upvotes: 0

Aaron Wallentine
Aaron Wallentine

Reputation: 2496

I had this problem ... using Webdriver with Codeception. My solution was to run a javascript snippet to give the iframe a name, and then I could switch to it based on name ... details here: https://stackoverflow.com/a/48123837/1593026

Upvotes: 0

salvu
salvu

Reputation: 519

If anyone using RSelenium in R happens on this little problem, this is what I used:

    webElement <- remDr$findElement(using = "css selector", "#dsq-app1")
    remDr$switchToFrame(webElement)
    # note: remDr is the remoteDriver instance that needs to be opened at the beginning

or

   webElement <- remDr$findElement(using = "id", value = "dsq-app1")
   remDr$switchToFrame(webElement) 

Upvotes: 1

alecxe
alecxe

Reputation: 474191

Aside from providing frame name or id, you can switch to the frame by index (zero-based):

Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index "0", the second at index "1" and the third at index "2". Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.

driver.switchTo().frame(0);  // assuming this is the first frame on the page

Or, you can make a WebElement instance by locating the iframe, for example, by CSS selector:

WebElement frame = driver.findElement(By.cssSelector("div#eyein-modal iframe"));
driver.switchTo().frame(frame);

See also:

Upvotes: 9

Related Questions