如何使用selenium从javascript(iframe)中提取文本?

时间:2015-08-13 21:28:59

标签: javascript python selenium iframe

当我拉取页面源时,我得到了下面的内容,但在实际网站上,我看到了我想要插入html的实际文本。

我假设我想要的文字可能隐藏在iframe中,所以我尝试了以下内容:

iframes = driver.find_elements_by_tag_name("iframe")
for iframe in iframes
    driver.switch_to_default_content()
    driver.switch_to_frame(iframe)

    output = driver.page_source
    print(output)

以下是我看到的页面的结果

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow: hidden;"><head><title>
    ITMS
</title><meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" /><meta content="IE=8" http-equiv="X-UA-Compatible" /><script type="text/JavaScript" language="JavaScript">
var cols = "200,*";
if (GetCookie("TreeviewFrameCols") != null) {
    var c = GetCookie("TreeviewFrameCols");
    if (c.indexOf(',') != -1) {
        cols = GetCookie("TreeviewFrameCols");
    }
}
function GetCookie(name)
{
    if (document.cookie == null)
        return null;
    var cookies = document.cookie.split(";");
    if (cookies == null || cookies.length == 0)
        return null;
    var i;
    for (i = 0; i &lt; cookies.length; i++)
    {
        var token = cookies[i].split("=");
        if (token != null &amp;&amp; token[0] != null)
        {
            var tokenName = token[0].replace(/^\s*(.*\S|.*)\s*$/, '$1');
            if (name == tokenName)
                return unescape(token[1]);
        }
    }
    return null;
}
function MM_displayStatusMsg(msgStr)
{
  status = msgStr;
  document.MM_returnValue = true;
}
function NavigateToUrl(target, url)
{
    var newUrl;
    if (url != "")
    {
        newUrl = url.toLowerCase();
        if ((newUrl.search("javascript:") != -1) || (newUrl.search("vbscript:") != -1))
        {
            eval(url);
            return;
        }
        else
            newUrl = url;
        if (target != null &amp;&amp; target != "")
        {
            if (eval(target) != null)
                eval((target + ".navigate('" + newUrl + "');"));
            else
                window.open(newUrl);
        }
        else
            window.open(newUrl);
    }
}
function CalculateFrameHeight() {
    var otherFramesTotalHeight = 80 + 25 + 25;
    var documentHeight = document.documentElement.clientHeight;
    var result = documentHeight - otherFramesTotalHeight;
    return result;
}
function SetTreeFrameUrl() {
    if (document.getElementById) {
        var frame = document.getElementById("TreeAdminFrame");
        if (frame != null) {
            var isMSIE = /*@cc_on!@*/0;
            var contentHeight = CalculateFrameHeight() - 4 - 29;
            if (!isMSIE) {
                contentHeight = CalculateFrameHeight() - 29;
            }
            frame.src = "https://rdc.svbconnect.com/shared/TreeAdmin.aspx?contenturl=https%3a%2f%2frdc.svbconnect.com%2fDetailedReport%2fMainContent.aspx" + "&amp;contentHeight=" + contentHeight;
        }
    }
}
function SetContentFrameUrl() {
    if (document.getElementById) {
        var frame = document.getElementById("AdminMainContentFrame");
        if (frame != null) {
            frame.src = "https://rdc.svbconnect.com/DetailedReport/MainContent.aspx";
        }
    }
}
document.write("&lt;frameset rows=80,25,*,25 cols=\"*\" framespacing=\"0\" frameborder=\"0\" border=\"0\" onLoad=\"MM_displayStatusMsg('ITMS');return document.MM_returnValue\"&gt;");
document.write("&lt;frame src=https://rdc.svbconnect.com/shared/HeaderFrameset.aspx?app=DetailedReport name=\"HeaderFrame\" frameborder=\"0\" scrolling=\"no\" noresize marginwidth=\"0\" marginheight=\"0\" id=\"HeaderFrame\"&gt;");
document.write("&lt;frame src=https://rdc.svbconnect.com/shared/ITMSMsgBar.aspx name=\"MsgFrame\" frameborder=\"0\" scrolling=\"no\" noresize marginwidth=\"0\" marginheight=\"0\" id=\"MsgFrame\"&gt;");
document.write("&lt;frameset cols=\"" + cols + "\" framespacing=\"2\" frameborder=\"1\" border=\"2\" id=\"AdminFrameset\"&gt;");
document.write("&lt;frame src=\"about:blank\" name=\"TreeAdminFrame\" frameborder=\"1\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" id=\"TreeAdminFrame\"&gt;");
document.write("&lt;frame src=\"about:blank\" name=\"AdminMainContentFrame\" frameborder=\"0\" scrolling=\"auto\" marginwidth=\"0\" marginheight=\"0\" id=\"AdminMainContentFrame\"&gt;");
document.write("&lt;/frameset&gt;");
document.write("&lt;frame src=https://rdc.svbconnect.com/shared/ITMSFooter.aspx name=\"FooterFrame\" frameborder=\"0\" scrolling=\"no\" noresize marginwidth=\"0\" marginheight=\"0\" id=\"FooterFrame\"&gt;");
document.write("&lt;/frameset&gt;");
SetTreeFrameUrl();
SetContentFrameUrl();
        </script></head>
  <frameset onload="MM_displayStatusMsg('ITMS');return document.MM_returnValue" border="0" frameborder="0" framespacing="0" cols="*" rows="80,25,*,25"><frame id="HeaderFrame" marginheight="0" marginwidth="0" noresize="noresize" scrolling="no" frameborder="0" name="HeaderFrame" src="https://rdc.svbconnect.com/shared/HeaderFrameset.aspx?app=DetailedReport" /><frame id="MsgFrame" marginheight="0" marginwidth="0" noresize="noresize" scrolling="no" frameborder="0" name="MsgFrame" src="https://rdc.svbconnect.com/shared/ITMSMsgBar.aspx" /><frameset id="AdminFrameset" border="2" frameborder="1" framespacing="2" cols="200,*"><frame id="TreeAdminFrame" marginheight="0" marginwidth="0" scrolling="no" frameborder="1" name="TreeAdminFrame" src="https://rdc.svbconnect.com/shared/TreeAdmin.aspx?contenturl=https%3a%2f%2frdc.svbconnect.com%2fDetailedReport%2fMainContent.aspx&amp;contentHeight=635" /><frame id="AdminMainContentFrame" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" name="AdminMainContentFrame" src="https://rdc.svbconnect.com/DetailedReport/MainContent.aspx" /></frameset><frame id="FooterFrame" marginheight="0" marginwidth="0" noresize="noresize" scrolling="no" frameborder="0" name="FooterFrame" src="https://rdc.svbconnect.com/shared/ITMSFooter.aspx" /></frameset>


</html>

0 个答案:

没有答案