'Selenium Javascript - Access webelements within iframe programatically

I am trying to access web elements within iFrame programatically. Using below method to check the same

  • Compare the URL of elements with the base URI
  • If its same then web element is in main DOM otherwise its in iFrame
  • Use switchTo.frame() to switch to iFrame and perform actions

In above method, the following methods used to get URI of element

document.getElementsByTagName("a")[0].baseURI;

or

document.getElementsByClassName("w3-btn w3-border")[0].baseURI

Queries:

  1. Is there any function or method to get baseURI using xpath? I don't want to use Tagname or classname.

  2. Also, how to achieve same when multiple iFrames or iFrame inside another iFrame.

  3. Kindly suggest if any other method



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source