通过使用Selenium Webdriver的Javascript执行程序中的Javascript XPATH获取iframe内容 [英] Get iframe content by using Javascript XPATH in Javascript executor for selenium webdriver

查看:146
本文介绍了通过使用Selenium Webdriver的Javascript执行程序中的Javascript XPATH获取iframe内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下代码移至框架,然后获取内容,但它返回的内容文档为空.

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);

另外,如果您可以指导我如何移动到帧,然后通过节点获取它的内容.iframe没有id,name等,因此我使用了xpath的方式.它正在将帧检测为结果XpathResult,但我是不知道如何进入它,然后为各个节点提取文本.必须使用Javascript.

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提供了一些html,但是尝试以下代码,则对您的帮助会更容易.只能使用JAVA来实现,而您不需要javascript.

It would be easier to help you if you provided some html with the iframes but try the following code. It can be achieved with JAVA only, you don't need javascript for this.

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();

希望对您有帮助!

这篇关于通过使用Selenium Webdriver的Javascript执行程序中的Javascript XPATH获取iframe内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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