如何单击 htmlunit 中的 javascript 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16614699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:21:27  来源:igfitidea点击:

How to click on a javascript button in htmlunit

javajavascripthtmlhtmlunit

提问by ManahManah

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

我正在尝试单击此网站中的搜索按钮:

http://www.amadeusepower.com/trek/portals/trek/default.aspx?Culture=en-US

http://www.amadeusepower.com/trek/portals/trek/default.aspx?Culture=en-US

The button is in here somewhere

按钮在这里某处

<table cellpadding="0" cellspacing="0" class="QuickSearchFormFlightModuleButtonsTable"
                width="100%">
                <tr>
                    <td class="cell1">
                        <a id="ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_hlFlightDetailedSearch" href="javascript:if(typeof notRedirectToTop == 'undefined'){document.forms[0].target = '_top';}__doPostBack('ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_hlFlightDetailedSearch', '');">Advanced options</a>
                    </td>
                    <td class="cell2">
                    </td>
                    <td class="cell3">

                    </td>
                    <td class="cell4">
                    </td>
                    <td class="cell5">
                        <script>DumpButtonHTML('ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch','QuickSearchModuleFlightSearchStartSearchButton','QuickSearchModuleFlightSearchStartSearch','javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl00$ctl00$cph1$cph1$QuickSearchAll1$QuickFlightSearchControl1$ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_LinkButton&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, 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');</script>
                    </td>
                </tr>
            </table>

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.

该按钮是站点左侧的搜索按钮。我使用了 Firefox 的 HtmlUnitScripter 插件来生成一个类,但即使它生成了填充表单的代码,它也不会生成单击按钮的代码。

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 pagevariable

按下按钮后,有一个加载屏幕,然后显示结果。通常,下一个代码应该将结果页面返回到page变量中

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=[<no source>] 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.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[<table id="ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table" onmouseout="this.className='QuickSearchModuleFlightSearchStartSearchButton';" border="0" width="100px" class="QuickSearchModuleFlightSearchStartSearchButton" cellpadding="0" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl00$ctl00$cph1$cph1$QuickSearchAll1$QuickFlightSearchControl1$ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_LinkButton&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, 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');StopEvent(event);" onmouseover="this.className='QuickSearchModuleFlightSearchStartSearchButtonOver';" style="cursor:pointer;cursor:hand;" cellspacing="0">] in http://www.amadeusepower.com/trek/portals/trek/default.aspx?Culture=en-US)
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.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?

我认为它正在尝试访问站点中的其他一些脚本,但它不在当前页面上。htmlunit 仍然可以通过某些功能访问这些脚本吗?

EDIT 2:

编辑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.

我在页面开头附近找到了以下脚本。这和搜索按钮有关系吗?它似乎有按钮的 ID,但我不确定。

</script>
<form name="aspnetForm" method="post" action="default.aspx?Culture=en-US" onsubmit="javascript:return WebForm_OnSubmit();" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch')" id="aspnetForm">
  <div>
    <input type="hidden" name="__WPPS" id="__WPPS" value="s"/>
    <input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value=""/>
    <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value=""/>
    <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value=""/>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPaA8FDzhkMDIyYzJhNTQzOGU2NBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WBAVXY3RsMDAkY3RsMDAkY3RsMDAkY3BoMSRjcGgxJFF1aWNrU2VhcmNoQWxsMSRRdWlja0ZsaWdodFNlYXJjaENvbnRyb2wxJGNoa0NhbGVuZGFyU2VhcmNoBUxjdGwwMCRjdGwwMCRjdGwwMCRjcGgxJGNwaDEkUXVpY2tTZWFyY2hBbGwxJFF1aWNrRmxpZ2h0U2VhcmNoQ29udHJvbDEkY2hrRmlmBVJjdGwwMCRjdGwwMCRjdGwwMCRjcGgxJGNwaDEkUXVpY2tTZWFyY2hBbGwxJFF1aWNrRmxpZ2h0U2VhcmNoQ29udHJvbDEkY2hrQWRkZmFtaWx5BVFjdGwwMCRjdGwwMCRjdGwwMCRjcGgxJGNwaDEkUXVpY2tTZWFyY2hBbGwxJFF1aWNrRmxpZ2h0U2VhcmNoQ29udHJvbDEkY2hrRm9yY2VQVEMp3jYJ9QEgzYItUgLEnRZgeEkhsA=="/>
  </div>
  <script type="text/javascript">

回答by tariq.freeman

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.

如果这不是您要查找的内容,并且您已经处理了点击部分,我建议尝试等待页面和 javascript 加载和执行。

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

回答by brnfd

The click function is in the table element, try this(didn't try on my own):

点击功能在table元素中,试试这个(我自己没试过):

HtmlElement theElement5 = (HtmlElement) page.getElementById("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table");
page = (HtmlPage) theElement5.click();

is this don't work, you can enable log on debug level to se whats happening, or try this(works on FF):

这是行不通的,您可以在调试级别启用登录以查看正在发生的事情,或尝试此操作(适用于 FF):

ScriptResult result = page.executeJavaScript("document.getElementById('ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch_Table').onclick()");
final Page newPage = result.getNewPage();

Hint: use Firebug to check the page variables and elements, and page.asXml() to get the real state of the page.

提示:使用 Firebug 检查页面变量和元素,使用 page.asXml() 获取页面的真实状态。