Reputation: 11
I have an html page with <embed src=......>
. Inside it #document
is used and nested html page is there. How to find elements by XPath inside #document
I am trying using JavaScript
WebElement element = driver.findElement(By.xpath("//div[contains(@id,'Recaptcha')]')]"));
Upvotes: 1
Views: 2295
Reputation: 137133
XPath won't be able to access the content of replaced elements from the main document.
If your content is loaded in accordance with Same-Origin-Policies, then you may use XPath from the replacing document directly. But <embed> is a weird beast...
Unlike <object> or <iframe> elements, it doesn't expose a contentDocument
property, but instead a getSVGDocument()
method. But as its name says, this method is intended to get an SVG Document, and some browsers (at least Chrome) will refuse to give you anything from an HTML document (while Firefox is ok with it)
So to have it to work in these browsers, you would need to use an other element, for instance an <iframe>. From there, you should be able to get its contentDocument and set it as the root of your XPath query:
iframe.onload = e => {
const doc = iframe.contentDocument; // get the contentDocument
// evaluate XPath
const query = doc.evaluate("//div[contains(@id,'Recaptcha')]')]", doc);
const node = query.iterateNext();
// ... do something with node
}
As a fiddle since Stack-Snippets do not allow SOP-friendly frames.
Upvotes: 2