Python selenium打印帧源

时间:2017-03-29 13:28:41

标签: python selenium frame

这是我第一次涉足Selenium。如果这是一个愚蠢/琐碎的问题,请提前道歉。

我正在尝试从网页上抓取信息。使用Python / Selenium,我可以登录该站点并访问包含我需要的信息的页面。在显示我需要的页面后,我正在发布

time.sleep(20)
html_source = driver.page_source
print html_source

"来源"被打印的不同于 右键单击并选择查看页面源和 右键单击并选择此框架,查看框架源

所需信息位于View Frame源中。所有这一切都在Firefox中。

到达Frame Source需要做什么?帧源中没有帧名称。

以下附加信息:

当我右键单击并选择查看页面源时,我得到以下内容:

<!DOCTYPE html><html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>xxxxxxx Portal</title>
      <base href="https://website.org/page/">
      <link rel="shortcut icon" href="images/logos/xxxxxxx.ico">
      <meta http-equiv="Pragma" content="no-cache">
      <meta http-equiv="Expires" content="-1"><script type="text/javascript" src="https://website.org/page/security/csrf.js"> </script><script type="text/javascript" src="https://website.org/page/security/csrf/execute.js"> </script><script>
                    function pushFocus()
                    {
                        frameDetail.focus();
                    }

                    function addInProgressPanel(doc)
                    {
                        var d = doc.createElement('div');
                        d.id="inProgressPane";
                        d.className="freezeOn";

                        var tbl = doc.createElement("table");
                        var row = tbl.insertRow(-1);
                        var oi = doc.createElement("img");
                        oi.src= 'https://website.org/page/'+ "images/actions/loading2.gif";
                        var td = doc.createElement("td");
                        td.className="detailFormField";
                        td.bgcolor="red";
                        td.appendChild(oi);
                        row.appendChild(td);

                        td = doc.createElement("td");
                        td.className="inProcessing";
                        td.appendChild(doc.createTextNode("Your Request is Being Processed  ..."));
                        row.appendChild(td);

                        d.appendChild(tbl);
                        doc.body.appendChild(d);
                        return d;
                    }

                    function inProgressScreen(type)
                    {
                        var ws = frames["frameDetail"];
                        if(!ws) return true;
                        var ips = ws.document.getElementById("inProgressPane");
                        if(ips)
                        {
                            if(type)  ips.className = 'freezeOn';
                            else      ips.className = 'freezeOff';
                        }else if(type)
                            ips = addInProgressPanel(ws.document);
                    }
                </script></head>
   <frameset id="main" framespacing="0" frameborder="0">
      <frame id="frameDetail" name="frameDetail" scrolling="auto" marginwidth="0" marginheight="0" src="portal/portal.xsl?x=portal.PortalOutline&amp;lang=en&amp;mode=notices">
   </frameset>
</html>

当我右键单击并选择This Frame,View Frame source I get

<!DOCTYPE html><html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <base href="https://website.org/xxxxxx/">
      <meta http-equiv="Content-Language" content="en-us">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta http-equiv="Pragma" content="no-cache">
      <meta http-equiv="Expires" content="-1">
      <title>xxxxxxxx Portal</title>
      <link rel="stylesheet" type="text/css" href="styles/portal/menu.css">
      <link rel="stylesheet" type="text/css" href="styles/portal/header.css">
      <link rel="stylesheet" type="text/css" href="styles/portal/footer.css">
      <link rel="stylesheet" type="text/css" href="styles/portal/jquery-ui-1.8.7.portal.css">
      <link rel="stylesheet" type="text/css" href="styles/portal/fg.menu.css">
      <link rel="stylesheet" type="text/css" href="styles/portal/portal.css">
      <link rel="stylesheet" type="text/css" href="styles/icons.css">
      <link rel="stylesheet" type="text/css" href="styles/portal/notifications.css"><script type="text/javascript" src="https://website.org/xxxxxxxx/security/csrf.js"> </script><script type="text/javascript" src="https://website.org/xxxxxxxx/security/csrf/execute.js"> </script><script src="scripts/widgets/common.js"></script><script src="scripts/controller.js"></script><script src="scripts/portal.js"></script><script src="scripts/jquery/jquery-1.7.2.min.js"></script><script type="text/javascript" src="https://website.org/xxxxxxxx/security/csrf/jquery.js"> </script><script src="scripts/jquery/jquery-ui-1.8.16.min.js"></script><script src="scripts/jquery/fg.menu.js"></script><script src="portal/lang/datePickerLanguage.jsp?lang=en"></script><script src="portal/portal.js"></script><script src="portal/portalNoShim.js"></script><script>

这里有更多代码。没贴,因为它太长了。除了以下对iSessionFrame的引用之外,没有框架名称:

    </script><script language="javascript" src="portal/grades.js"></script></div>
            </div>
         </div>
         <div id="footer">
            <table id="language"><select id="locale" style="width:175px"></select></table>
         </div>
      </div><iframe id="iSessionFrame" name="iSessionFrame" width="0" height="0" src="https://website.org/xxxxxx/white.jsp" style="visibility:hidden;"></iframe></body>
</html>

2 个答案:

答案 0 :(得分:1)

问:我需要做什么才能到达Frame Source?

A:首先,您必须使用switch_to命令切换到所需的帧,然后您应该使用.page_source来获取html源。

Obs。:查看Selenium文档,更具体地说是Moving between windows and frames

代码:

driver.switch_to_frame(driver.find_element_by_tag_name("frameDetail"))
driver.page_source

答案 1 :(得分:0)

您可以尝试使用其ID切换到框架:

driver.switch_to_frame(driver.find_element_by_id("iSessionFrame"))
driver.page_source