1

I have some selenium web driver code which fails to identify any web elements between lines 23 and 81. I am however able to identify and interact with all web elements above line 23 and below 81.

19 ….
20 <div id="main" class="well content" ui-view="content">
21     <iframe src="swagger-ui/index.html" frameborder="0" marginheight="0" marginwidth="0"  
22      width="100%" height="900" scrolling="auto" target="_top" title="Swagger UI">
23           #document
24                <!doctype html>
25           <html>
26           <head>…</head>
27           <body class= “swagger-section” marginWidth= “0” marginheight= “0”>
                      <div class = “swagger-ui-wrap”>
                           <a id =”logo” href=http://swagger.io>swagger</a>
                                 …
                      </div>
80            </body>
81     </iframe>
82 </div>
83  ….

I’ve added the following maven dependency to my POM:

<!-- https://mvnrepository.com/artifact/io.swagger/swagger-core -->
<dependency>
   <groupId>io.swagger</groupId>
   <artifactId>swagger-core</artifactId>
   <version>1.5.21</version>
</dependency>

Could someone please let me know if there are additional dependencies I need to include in my POM or provide some insight as to why size = 0 in the below statement?

size = driver.findElements(By.xpath("//*[@id='swagger-ui-container']")).size();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
e44
  • 55
  • 3

1 Answers1

1

SeleniumWebDriver is unable to identify any web elements between lines 23 and 81 as those lines:

21     <iframe src="swagger-ui/index.html" frameborder="0" marginheight="0" marginwidth="0"  
22      width="100%" height="900" scrolling="auto" target="_top" title="Swagger UI">
23           #document
24                <!doctype html>
25           <html>
26           <head>…</head>
27           <body class= “swagger-section” marginWidth= “0” marginheight= “0”>
              <div class = “swagger-ui-wrap”>
               <a id =”logo” href=http://swagger.io>swagger</a>
                 …
              </div>
80            </body>
81     </iframe>

are within an <iframe>.


To identify and interact with all the WebElements within an <iframe> tag you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be visible.
  • You can use either of the following solutions:

    • cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Swagger UI'][src='swagger-ui/index.html']")));
      my_size = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#swagger-ui-container"))).size();
      
    • xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@title='Swagger UI' and @src='swagger-ui/index.html']")));
      my_size = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id='swagger-ui-container']"))).size();
      

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352