How do I click a javascript button with htmlunit?

前端 未结 3 1223
孤街浪徒
孤街浪徒 2020-12-19 22:46

I\'m working on an application that will automatically click a button on a webpage using htmlunit in Java. Only problem is that that button is a javascript button, so the st

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 23:16

    I have a very similar link on one of my pages. If you can call .click() on any HtmlElement, it should be able to run associated Javascript. Here is my code (generated from HtmlUnitScripter):

    HtmlElement element4 = null;
    Iterable iterable5 = page.getAllHtmlChildElements();
    Iterator i6 = iterable5.iterator();
    while(i6.hasNext())
    {
        HtmlElement anElement = i6.next();
        if(anElement instanceof HtmlImage)
        {
            HtmlImage input = (HtmlImage) anElement;
            String[] elements = "http://example.com/pages/powerbutton.png".split( "/" );
    
            if(input.getSrcAttribute().indexOf(elements[elements.length-1] )> -1 )
            {
                element4 = input;
                break;
            }
        }
    }
    
    HtmlPage page = element4.click();
    

提交回复
热议问题