I am currently attempting to access web elements within an iFrame programmatically. I am using the following method to verify this:
- Check the URL of elements against the base URI
- If they match, the web element is in the main DOM; if not, it is located in the iFrame
- Utilize switchTo.frame() to navigate to the iFrame and interact with the elements inside
The above method involves the use of the following codes to obtain the URI of an element:
document.getElementsByTagName("a")[0].baseURI;
or
document.getElementsByClassName('w3-btn w3-border')[0].baseURI
Inquiries:
Is there a function or method available to retrieve the baseURI using XPath? I prefer not to rely on Tagname or class name.
Furthermore, how can this be accomplished when dealing with multiple iFrames or an iFrame nested within another iFrame?
Your suggestions for alternative methods are welcomed.