[英]Get iframe content by using Javascript XPATH in Javascript executor for selenium webdriver
I am trying the following code to move to frame and then get the content but it is returning empty content document. 我正在尝试下面的代码移动到框架,然后获取内容,但它返回的是空内容文档。
var iframe = document.evaluate('//iframe[contains(@src, \"//my.leadpages.net/forms/5620062778032128/5654100301578240/5645914630782976/html/?lp-in-iframe=1\")]',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).contentDocument;alert(iframe);
Also if you can guide me through for how can I move to frame and then get it's contents via nodes.Iframe has no id,name etc so I have used xpath way.It is detecting the frame as resulting XpathResult but I am not sure how to move into it and then extract text for various nodes.This has to be in Javascript. 另外,如果您可以指导我如何移动到帧然后通过节点获取它的内容.iframe没有id,name等,则我使用了xpath的方式。它正在检测帧作为结果XpathResult但我不确定如何移到其中,然后为各个节点提取文本。这必须使用Javascript。
It would be easier to help you if you provided some html with the iframes but try the following code. 如果您在iframe中提供了一些html,但是尝试以下代码,则对您的帮助会更容易。 It can be achieved with JAVA only, you don't need javascript for this.
只能使用JAVA来实现,您不需要javascript。
driver.switchTo().frame(driver.findElement(By.xpath("frame xpath expression here")));
//do your stuff here.
//to get the content of this frame, it depends on the structure of the html
//if content is within some div then simply do this.
//this would be the css selector for some DIV where the content is inside the iframe:
//div#divContentID
WebElement divContent=driver.findElement(By.cssSelector("div#divContentID"));
String content=divContent.getText();
//to switch out of the iframe back to the main content.
driver.switchTo().defaultContent();
I hope it helps!! 希望对您有所帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.