【问题标题】:Htmlunit Javascript Button/LinkHtmlunit Javascript 按钮/链接
【发布时间】:2013-06-26 23:07:37
【问题描述】:

我正在尝试查找特定来​​源的链接/按钮。我已经设法使用“表单”属性分配了一个按钮,但是我尝试按下的新按钮没有表单。下面是 javascript 按钮的 HTML 源代码:

<span class="followButtonActions" id=".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].   [1].[1]">
    <a class="Button FollowButton followButtonFollow" role="button" href="javascript:;" id=".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0">
        <span id=".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0.[0]">
        <span id=".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0.[0].[0]">Follow</span>    </span>
    </a>
</span>

到目前为止,我一直在尝试使用该类来识别元素,但是我未能成功找到页面上的链接/按钮。 我一直在尝试使用 htmlAnchor,但是没有用,所以我切换到 DOM ELEMENT。 以下是我当前用于查找结果的按钮/链接的代码:java.lang.NullPointerException。

final DomElement myAnchor = page3.getFirstByXPath("//span[@class='followButtonActions']");
final HtmlPage newPage = myAnchor.click();

我还尝试了以下结果:java.lang.NullPointerException。

    final HtmlSubmitInput button = page.getFirstByXPath("//class[@id='.reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0']");
    final HtmlPage newPage = button.click(); 

我已经到了几乎尝试所有东西的地步!用外行的话来说,我试图将 javascript 链接分配给一个按钮,然后可以按下该按钮。我的方法是以某种方式将“Button FollowButton followButtonFollow”链接到一个可以单击的按钮。任何帮助将不胜感激。谢谢你:)

【问题讨论】:

    标签: java javascript button htmlunit


    【解决方案1】:

    如果您尝试获取该锚标记,则应使用 XPath:

    //a[@id='.reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0']
    

    查看下面的演示代码:

    import java.net.URL;
    import com.gargoylesoftware.htmlunit.*;
    import com.gargoylesoftware.htmlunit.html.*;
    public class HtmlUnitAnchorExample {
        public static void main(String[] args) throws Exception {
            String html = "<html><head><title>HTMLUNIT TEST</title></head><body>" +
                    "" +
                    "<span class=\"followButtonActions\" id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].   [1].[1]\"> "+
                    " <a class=\"Button FollowButton followButtonFollow\" role=\"button\" href=\"javascript:;\" id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0\"> "+
                    "     <span id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0.[0]\"> "+
                    "     <span id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0.[0].[0]\">Follow</span>    </span> "+
                    " </a> "+
                    "</span>" +
                    "</body></html>";
    
            WebClient client = new WebClient();
            HtmlPage page = HTMLParser.parseHtml(new StringWebResponse(html, new URL("http://example.com")), client.getCurrentWindow());
            System.out.println("Page title: "+page.getTitleText());
    
            final HtmlAnchor myAnchor = page.getFirstByXPath("//a[@id='.reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0']");
            System.out.println("Anchor found: "+myAnchor );
            final HtmlPage newPage = myAnchor .click();
            System.out.println("newPage: "+newPage);
    
            client.closeAllWindows();
        }
    }
    

    【讨论】:

    • 感谢您的回复。我已经应用了您的代码,但是我的锚点打印为 NULL。由于某种原因,当我查看源代码时,我可以查看 但是,当我使用 page.asXml() 打印页面时,我无法查看 等等。你能提出任何想法吗?
    • 在调用page.asXml() 之前先执行webClient.waitForBackgroundJavaScript(10 * 1000);。从您所说的来看,源页面似乎是通过 javascript 创建 thos 元素(您发布的)。是这样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多