简体   繁体   English

通过在Selenium Webdriver的Javascript执行程序中使用Javascript XPATH获取iframe内容

[英]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.

相关问题 使用Selenium WebDriver和JavaScript从XPath找到Element的文本 - Get text from XPath located Element using Selenium WebDriver with JavaScript 使用 Selenium WebDriver 和 JavaScript 从 XPath 元素获取文本 - Get text from XPath Element using Selenium WebDriver with JavaScript 有没有办法在 Selenium WebDriver 中使用 JavaScript 通过 XPath 获取元素? - Is there a way to get element by XPath using JavaScript in Selenium WebDriver? 在硒javascript执行器中使用javascript - using javascript in selenium javascript executor 如何通过XPath从Selenium WebDriver中的JavaScript代码获取特定文本 - How to get a specific text from a javascript code in selenium webdriver by xpath 使用 JavaScript 获取 iframe 内容 - Get iframe content by using JavaScript 如何使用Jasmine JavaScript在Selenium Webdriver中获取XPath计数? - How can i get xpath count in selenium webdriver using jasmine javascript? 在 selenium webdriver 上的 javascript 中使用 xpath() 计算元素数量 - Count number of elements using xpath() in javascript on selenium webdriver 如何使用xpath和Javascript执行程序打印文本 - How to print text using xpath and Javascript executor Selenium WebDriver:使用JavaScript Executor验证是否选中了复选框 - Selenium WebDriver : Verify whether a check-box is selected or NOT using JavaScript Executor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM