HtmlUnit unable to get frame content

时光毁灭记忆、已成空白 提交于 2020-01-24 20:13:13

问题


I am trying to set the value of a search box, to click a search button and to parse the results. The problem is that the results are displayed in another frame and I am not able to obtain the other frame. The code:

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();
          }
       }    
    }

回答1:


You may try to get that frame using next code:

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

After getting source of the frame you may get html elements (like you did on login page) inside this page.




回答2:


You can try this example (taken from http://htmlunit.sourceforge.net/frame-howto.html)

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

To get the page of the first frame (at upper left) and click the sixth link:

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

To get the page of the frame named 'packageFrame' (at lower left) and click the second link:

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

To get the page of the frame named 'classFrame' (at right):

final HtmlPage classPage = (HtmlPage) mainPage.getFrameByName("classFrame").getEnclosedPage();


来源:https://stackoverflow.com/questions/17891852/htmlunit-unable-to-get-frame-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!