gpt4 book ai didi

java - HtmlUnit 等待重定向

转载 作者:行者123 更新时间:2023-12-01 17:55:49 25 4
gpt4 key购买 nike

我需要在 HtmlUnit 中实现以下场景:


-我点击按钮
-我被重定向到页面 www.page.pl/result
-我需要等待几秒钟,然后我会被重定向到 www.anotherpage.pl/auth_code=qwe123

问题是,一旦我进入/result 页面,我就无法退出到下一个页面。那里没有手动操作,我只需要等到重定向发生

这是我的WebClient

private WebClient initWebClient() {
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
return webClient;
}

为了实现这一目标,我尝试了多种方法,包括 Thread.sleep()已经...

 HtmlPage page = webClient.getPage(url);
page.getForms().get(0).getButtonByName("submit").click(); // Redirects me to /result
webClient.waitForBackgroundJavaScript(5000);
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
Thread.sleep(5000);
page.refresh(); //I'm still on /result, while I need to be on completely new page by now

最佳答案

如果其他人有类似的问题,我找到了解决方案

    webClient.addWebWindowListener(new WebWindowListener() {
@Override
public void webWindowOpened(WebWindowEvent webWindowEvent) {

}

@Override
public void webWindowContentChanged(WebWindowEvent webWindowEvent) {
if (webWindowEvent.getNewPage() != null) {
// do what you need to do with webWindowEvent.getNewPage().getUrl();
}
}

@Override
public void webWindowClosed(WebWindowEvent webWindowEvent) {

}
});

关于java - HtmlUnit 等待重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60719908/

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