如何找出iframe的xpath.下面是图片 [英] How to find out xpath for iframes.Below is the picture attached

查看:119
本文介绍了如何找出iframe的xpath.下面是图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在下图中找到文字与您的搜索相关的品牌"

I need to find out text " brands related to your search" in below image

我该如何写xpath.该文本存在于没有ID或类或唯一标识符的iframe中.我当时想使用跨度数据组件ID来找到它,但它不起作用.

How can i write the xpath.The text is present in an iframe which has no id or class or no unique identifier.I was thinking to find it using span data component id but it did not work.

By.xpath("//span [@ data-component-id = '36']/iframe

By.xpath("//span[@data-component-id='36']/iframe

请帮助

https://i.stack.imgur.com/7EW5n.png

推荐答案

您不能遍历<iframe>上的元素,它位于不同的上下文中.您需要激活或切换到iframe的上下文,例如.使用JavaScript与iframe及其内部的文档进行交互.

You cannot traverse the elements on an <iframe>, it lives in a different context. You need to activate or switch to the context of the iframe, eg. using JavaScript to interact with an iframe and the document inside of it.

//Note: Assigning document.domain is forbidden for sandboxed iframes, i.e. on stacksnippets
//document.domain = "https://stacksnippets.net";

var ifrm = document.getElementById("myFrame");
// reference to iframe's window
//var win = ifrm.contentWindow;
// reference to document in iframe
var doc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
// reference an element via css selector in iframe
//var form = doc.getElementById('body > div > div.message');
// reference an element via xpat in iframe
var xpathResult = doc.evaluate("/html/body/div/div[1]", doc, null, XPathResult.ANY_TYPE, null);

<iframe id="myFrame" src="https://stacksnippets.net" style="height:380px;width:100%"></iframe>

但是,正如您看到的那样,在运行摘要后,跨文档交互仅在文档具有相同原点的情况下才可行.还有其他更复杂的方法,例如 postMessage 方法提供了这种方法跨域交互的作用.

However, as you can see when you run the snipped, cross-document interactions are only possible if the documents have the same origin. There are other, more involved methods like the postMessage method that provide the means of interacting cross-domain.

这篇关于如何找出iframe的xpath.下面是图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆