簡體   English   中英

HTMLunit Javascript按鈕/鏈接

[英]Htmlunit Javascript Button/Link

我正在嘗試查找特定來​​源的鏈接/按鈕。 我已經設法使用'form'屬性分配按鈕,但是我嘗試按下的新按鈕沒有表單。 以下是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”鏈接到可以單擊的按鈕。 任何幫助將不勝感激。 謝謝 :)

如果嘗試獲取該錨標記,則應使用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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM