Alex
Alex

Reputation: 3

How to Access Web Elements in Nested HTML Tags Using Selenium

I have a website that generates a report within an iframe, and the website's HTML structure is laid out to look like the following:

<html> <!-- a -->
  <body>
    .
    . (C)
    .
    <iframe>
      <html> <!-- b -->
        <body>
        .
        . (D)
        .
        </body>
      </html>
    </iframe>
  </body>
</html>

I am wondering how I can access elements in the body containing "(D)" from above. So far I have tried using xpath, but if I copy the xpath from an element in (D) in begins from html tag b, so when the program starts searching from html tag a, it finds nothing. I tested to see if the has any child web elements, and it has 0. Attempts to search for elements in (D) by ID have also also resulted in NoSuchElementException. Not a time problem because I am using WebDriverWait for 120 seconds to make sure everything on page has loaded. Thank you for any help you can provide.

Upvotes: 0

Views: 238

Answers (1)

DMart
DMart

Reputation: 2461

Because it's in an iframe you need to use switchTo to grab the DOM of the frame. See more here: https://www.techbeamers.com/switch-between-iframes-selenium-python/ or https://www.guru99.com/handling-iframes-selenium.html

driver.switch_to.iframe(self,frame reference)

where reference is:

  • By using the tag name ( in this case ‘iframe’)
  • By using the Id of IFrame
  • By using the name of IFrame

    Upvotes: 1

  • Related Questions