[英]Htmlunit Javascript Button/Link
I am trying to find the link/button for a specific source. 我正在尝试查找特定来源的链接/按钮。 I have already managed to assign a button using the 'form' properties, however the new button I am trying to press does not have a form. 我已经设法使用'form'属性分配按钮,但是我尝试按下的新按钮没有表单。 Below is the HTML source code for the javascript button: 以下是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>
So far I been trying to use the class to identify the element, however I have been unsuccessful in finding the link/button on page. 到目前为止,我一直在尝试使用该类来标识元素,但是在页面上找到链接/按钮一直没有成功。 I have been trying to use htmlAnchor however that didn't work so I switched to DOM ELEMENT. 我一直在尝试使用htmlAnchor,但是那没有用,所以我切换到DOM ELEMENT。 Below is my current code for finding the button/link which results: java.lang.NullPointerException. 下面是我当前用于查找结果的按钮/链接的代码:java.lang.NullPointerException。
final DomElement myAnchor = page3.getFirstByXPath("//span[@class='followButtonActions']");
final HtmlPage newPage = myAnchor.click();
I have also tried the following which results in: java.lang.NullPointerException. 我还尝试了以下结果: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();
I have got to a point where I am trying almost everything! 我已经到了要尝试几乎所有东西的地步! In laymans terms, I am trying to assign the javascript link to a button which can then be pressed. 用外行术语来说,我试图将javascript链接分配给一个按钮,然后可以将其按下。 My approach was to somehow link the 'Button FollowButton followButtonFollow' to a button which can be clicked. 我的方法是以某种方式将“ Button FollowButton followButtonFollow”链接到可以单击的按钮。 Any help will be appreciated. 任何帮助将不胜感激。 Thank you :) 谢谢 :)
If you are trying to get that anchor tag, you should use the XPath: 如果尝试获取该锚标记,则应使用XPath:
//a[@id='.reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0']
Check the demo code below: 检查下面的演示代码:
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.