HtmlUnit无法获取帧内容

时间:2013-07-26 22:11:25

标签: java htmlunit frameset

我正在尝试设置搜索框的值,单击搜索按钮并解析结果。问题是结果显示在另一个框架中,我无法获得另一个框架。代码:

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

import java.io.IOException;
import java.net.MalformedURLException;

public class LoginSimulation
{
     public static void main(String args[])
     {
          HtmlPage page = null;
          String url = "http://www.ravmilim.co.il/naerr.asp";

          WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
          webClient.setThrowExceptionOnScriptError(false);

          try
          {
               page = webClient.getPage( url );

               HtmlTextInput userInput = (HtmlTextInput) page.getElementById("txtUser");
               userInput.setValueAttribute("yacov.schondorf@gmail.com");

               HtmlPasswordInput passwordInput = (HtmlPasswordInput) page.getElementById("txtPass");
               passwordInput.setValueAttribute("5750201");

               HtmlElement theElement2 = (HtmlElement) page.getElementById("submitButton");
               page = theElement2.click();  

              HtmlPage framePage = (HtmlPage)               nextPage.getFrames().get(0).getEnclosedPage(); 
              HtmlTextInput searchBox = (HtmlTextInput)                            framePage.getForms().get(0).getInputsByName("searchBox").get(0);

              //
              // so far so good...
              //
              searchBox.setValueAttribute("word");
              HtmlAnchor anchor = framePage.getHtmlElementById("sl");
              HtmlPage page1 = (HtmlPage) anchor.click(); 
              try { 
                  HtmlPage resultsPage = (HtmlPage) page1.getFrameByName("resault1").getEnclosedPage();// this should have worked!!
              } catch (Exception e) { 
                  //
                  // I get an ElementNotFoundException
                  //
                  e.printStackTrace(); 
              } 

              // 
              // must logout - this site is sensitive to multiple logins
              //
              framePage.getAnchorByHref("logout.asp").click();

              webClient.closeAllWindows();            
          }
          catch ( Exception e )
          {
               e.printStackTrace();
          }
       }    
    }

2 个答案:

答案 0 :(得分:1)

您可以尝试使用下一个代码获取该帧:

HtmlPage framePage = (HtmlPage)pageAfterLogin.getFrameByName("your_frame_name").getEnclosedPage();

获取框架的源代码后,您可以在此页面中获得html元素(就像您在登录页面上所做的那样)。

答案 1 :(得分:0)

您可以尝试此示例(取自http://htmlunit.sourceforge.net/frame-howto.html

final WebClient client = new WebClient();
final HtmlPage mainPage = client.getPage("http://htmlunit.sourceforge.net/apidocs/index.html");

获取第一帧的页面(在左上角)并单击第六个链接:

final HtmlPage packageListPage = (HtmlPage) mainPage.getFrames().get(0).getEnclosedPage();
packageListPage.getAnchors().get(5).click();

获取名为“packageFrame”的框架页面(左下角),然后点击第二个链接:

final HtmlPage pakcagePage = (HtmlPage) mainPage.getFrameByName("packageFrame").getEnclosedPage();
pakcagePage.getAnchors().get(1).click();

获取名为“classFrame”的框架页面(右侧):

final HtmlPage classPage = (HtmlPage) mainPage.getFrameByName("classFrame").getEnclosedPage();
相关问题