java中js点击按钮,如何点击htmlunit中的javascript按钮

I'm trying to click on a search button in this website:

The button is in here somewhere

width="100%">

Advanced options

DumpButtonHTML('ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch','QuickSearchModuleFlightSearchStartSearchButton','QuickSearchModuleFlightSearchStartSearch','javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$ctl00$cph1$cph1$QuickSearchAll1$QuickFlightSearchControl1$ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_LinkButton", "", true, "", "", true, true));LockButton(this,\'\',true);Loading(IsValidForTableButton(\'\',true),\'DefaultSplash_SplashScreen\',\'/trek/App_Themes/trek_theme1/Templates/SplashScreens/\',\'ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtFrom;ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtTo;ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtDepartureDate_txtDate;ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtReturnDate_txtDate\');','True','Search','100px','True','','trek_theme1');

The button is the search button on the left side of the site. I used HtmlUnitScripter add-on for firefox for generating a class but even though it generates the code that will fill the forms it doesn't generate the code that will click the button.

After the button is pressed, there is a loading screen and then the results are displayed. Normally this next code should return the results page into the page variable

HtmlElement theElement5 = (HtmlElement) page.getElementById("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch");

page = (HtmlPage) theElement5.click();

But it only returns the previous page with the forms filled. Is there a special way to handle this button or am I not finding the right button to click? Any help will be appreciated.

EDIT:

The exception I get when I used

ScriptResult result = page.executeJavaScript("document.getElementById('ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table').onclick()");

final Page newPage = result.getNewPage();

is below

Exception in thread "main" ======= EXCEPTION START ========

EcmaError: lineNumber=[64] column=[0] lineSource=[] name=[TypeError] sourceName=[http://www.amadeusepower.com/trek/Amadeus/js/Functions.js] message=[TypeError: Cannot read property "cancelBubble" from undefined (http://www.amadeusepower.com/trek/Amadeus/js/Functions.js#64)]

com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "cancelBubble" from undefined (http://www.amadeusepower.com/trek/Amadeus/js/Functions.js#64)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669)

at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601)

at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:555)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:530)

at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:979)

at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:947)

at htmlunittest.test.com.TestEkoBilet.main(TestEkoBilet.java:51)

Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot read property "cancelBubble" from undefined (http://www.amadeusepower.com/trek/Amadeus/js/Functions.js#64)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3608)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3624)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:3634)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getObjectPropNoWarn(ScriptRuntime.java:1488)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1233)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)

at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)

at com.gargoylesoftware.htmlunit.javascript.host.EventHandler.call(EventHandler.java:81)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1531)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)

at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)

at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:405)

at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:275)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031)

at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:546)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:654)

... 7 more

Enclosed exception:

net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot read property "cancelBubble" from undefined (http://www.amadeusepower.com/trek/Amadeus/js/Functions.js#64)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3608)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3624)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:3634)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getObjectPropNoWarn(ScriptRuntime.java:1488)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1233)

at script.StopEvent(http://www.amadeusepower.com/trek/Amadeus/js/Functions.js:64)

at script.onclick(onclick event for HtmlTable[

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)

at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)

at com.gargoylesoftware.htmlunit.javascript.host.EventHandler.call(EventHandler.java:81)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1531)

at script(injected script:1)

at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)

at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)

at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:405)

at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:275)

at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031)

at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:546)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:654)

at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601)

at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:555)

at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:530)

at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:979)

at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:947)

at htmlunittest.test.com.TestEkoBilet.main(TestEkoBilet.java:51)

======= EXCEPTION END ========

I think it is trying to access some other script in the site but it's not on the current page. Can htmlunit still access those scripts via some function or is this as far as it gets?

EDIT 2:

I found the following script near the beginning of the page. Does that have anything to do with the search button? It seems to have the id of the button but I'm not sure.

解决方案

try this if you are unable to click on the button

HtmlElement htmlElement = page.getFirstByXPath("//*[@id=\"ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table\"]/tbody/tr/td[2]");

htmlElement.click();

if this is not what you are looking for, and you already handled the click part

i suggest try wait for the page and javascript to load and execute.

try this

webClient.waitForBackgroundJavaScript(1000);

or

webClient.setAjaxController(new NicelyResynchronizingAjaxController());

webClient.setAjaxController(new AjaxController(){

@Override

public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)

{

return true;

}

});

or

JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();

while (manager.getJobCount() > 0) {

Thread.sleep(100);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值