gpt4 book ai didi

htmlunit - 在 HtmlUnit 中切换 url

转载 作者:行者123 更新时间:2023-12-04 04:51:30 33 4
gpt4 key购买 nike

有什么办法可以在 HtmlUnit 中使用 session 更改 url?

我的情况如下所示,

  • 登录 http://test.raja.com与凭据。
  • 获取页面http://home.raja.com ,它应该让我的主页登录。

  • 在浏览器中,这完全没问题。但是在 HtmlUnit 中,我只能得到 test.raja.com 而不是主页。我尝试了 WebWindow、WebConnection、addCookie 等,但没有任何效果。

    https://community.jboss.org/wiki/UsingTheHtmlUnitAPIWithJSFUnit 中找到了这个

    Getting a new URL in the same browser session

    If you want to simulate the user typing a URL into the browser session or need to do a GET request for any other reason, you need to use the WebClient and JSFUnit's WebConversationFactory as in this example:

    JSFSession jsfSession = new JSFSession("/index.jsf"); WebClient webClient = jsfSession.getWebClient(); webClient.getPage(WebConversationFactory.getWARURL() + "/myotherpage.jsf");

    JSFSession, JSFServerSession, and JSFClientSession will stay in sync with the WebClient and you can continue to use those objects normally. Also, the HttpSession will be the same because the WebClient will continue to set the JSESSIONID cookie.



    这在 HtmlUnit 本身中可能吗?

    我的示例代码
    final WebClient webClient = new WebClient();
    HtmlPage page = webClient.getPage("https://home.raja.com");
    final List<FrameWindow> window = page.getFrames();
    final HtmlPage pageTwo = (HtmlPage) window.get(0).getEnclosedPage();//this is test.raja.com served via iframe
    HtmlForm form = pageTwo.getFormByName("login");
    HtmlInput userName = (HtmlTextInput)form.getInputByName("testone");
    HtmlInput password = (HtmlPasswordInput)form.getInputByName("testtwo");
    userName.setValueAttribute("guest");
    password.setValueAttribute("guest");
    HtmlInput submit = (HtmlSubmitInput)form.getInputByName("submit");
    HtmlPage pagethree = (HtmlPage)submit.click();
    page = webClient.getPage("https://home.raja.com"); //here it again goes for login page

    最佳答案

    问题解决了。使用了以下代码,

    webClient.setRedirectEnabled(true);
    webClient.setJavaScriptEnabled(true);
    webClient.setThrowExceptionOnScriptError(false);
    webClient.setThrowExceptionOnFailingStatusCode(false);
    webClient.setUseInsecureSSL(true);
    webClient.setCssEnabled(true);
    webClient.setAppletEnabled(true);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.waitForBackgroundJavaScript(10000);

    关于htmlunit - 在 HtmlUnit 中切换 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17369707/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com