如何在Java(Selenium)中获取iframe的内容?

时间:2015-03-23 17:44:48

标签: java html selenium iframe

我需要在java中使用selenium API获取iframe的全部内容。

这是预期的结果:

<div id="WIN_0_304255502" arid="304255502" artype="View" ardbn="z2VF_02" 
        arcontainerid="304255802" arpercentwidth="100" 
        arpercentheight="100" class="arfid304255502 ardbnz2VF_02" 
        ardcf="1" style="top: auto; left: auto; width: 1318px; 
        height: 744px; z-index: 1135; min-width: 20px; 
        max-width: 32767px; min-height: 20px; 
        max-height: 32767px; position: relative; overflow: hidden;
        background-color: transparent;" 
        arvfframe="<iframe style=&quot;top:0px&amp;#59; left:0px&amp;#59; width:1170px&amp;#59; 
        height:744px&amp;#59;
        background-color: transparent&amp;#59;&quot; name=&quot;VF304255502&quot; frameborder=0 
        scrolling=&quot;no&quot; 
        allowtransparency=&quot;true&quot; arviewbordercolor=&quot;null&quot; title=&quot;z2VF_02&quot; src=&quot;javascript:&amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;/HTML&amp;gt;&amp;quot;&quot; 
        onload=&quot;DVFol&amp;#40;&amp;#41;&quot;>
        </iframe>
        " arwindowid="0" arstop="1">

        //more elements

现在我试试:

WebDriver driver= new HtmlUnitDriver();
driver.get(url3);
saveFile(driver.getPageSource());

运行程序后,我得到的结果不是我的预期。也许是因为存在iframe。

结果:

<div id="WIN_0_304255502" arid="304255502" artype="View" ardbn="z2VF_02" arcontainerid="304255802" 
        arpercentwidth="100" arpercentheight="100" class="arfid304255502 ardbnz2VF_02" 
        ardcf="1" style="top:0px; left:0px; width:1170px; height:744px;z-index:1135;background-color:transparent;min-width:20px;max-width:32767px;min-height:20px;max-height:32767px;" 
        arvfframe="&lt;iframe style=&quot;top:0px&amp;#59; left:0px&amp;#59; width:1170px&amp;#59; 
        height:744px&amp;#59;background-color: transparent&amp;#59;&quot; name=&quot;VF304255502&quot; 
        frameborder=0 scrolling=&quot;no&quot; allowtransparency=&quot;true&quot; arviewbordercolor=&quot;null&quot; 
        title=&quot;z2VF_02&quot; src=&quot;javascript:&amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;/HTML&amp;gt;&amp;quot;&quot; 
        onload=&quot;DVFol&amp;#40;&amp;#41;&quot;>
        &lt;/iframe>
    ">
     </div>

我也尝试访问iframe(driver.switchTo().frame(driver.findElement(By.name("VF304255503")),但发生异常(org.openqa.selenium.NoSuchElementException: Unable to locate element with name: VF304255503)。

0 个答案:

没有答案