我想在java中使用htmlUnit访问hotmail帐户的垃圾文件夹,因为htmlunit似乎支持javascript。使用以下代码,我可以访问收件箱文件夹,但我想从中获取垃圾文件夹。我发现页面左侧显示的“垃圾”是一个标签。所以我试图访问标签,然后想点击它。但我无法从 HtmlPage 访问“垃圾”标签。垃圾标签的 HTML 代码如下所示。
<span class="FolderLabel">
Junk
<span style="display:none;">
(
<span class="count"></span>
)
</span>
</span>
</span>
以下是我用来获取垃圾文件夹的代码:
import java.io.IOException;
import java.net.URL;
import org.w3c.dom.Node;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlLabel;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class TesthtmlUnit {
public static void main(String[] args) throws IOException
{
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("https://login.live.com/ppsecure/post.srf");
webClient.setThrowExceptionOnScriptError(false);
final HtmlForm form = page1.getFormByName("f1");
final HtmlSubmitInput button = form.getInputByName("SI");
final HtmlTextInput uname = form.getInputByName("login");
final HtmlPasswordInput pwd = form.getInputByName("passwd");
// Change the value of the text field
uname.setValueAttribute("emailid@hotmail.com");
pwd.setValueAttribute("pwd");
HtmlPage page2 = button.click();
//System.out.println(page2.asXml());
HtmlAnchor inbox = (HtmlAnchor) page2.getAnchorByHref("http://mail.live.com/?rru=inbox");
page2 = (HtmlPage) inbox.click();
//System.out.println(page2.asXml());
//CLICK ON JUNK LABEL
//here I want access label "junk" from page2
webClient.closeAllWindows();
}
}
有人知道如何点击垃圾文件夹吗?我经历了 htmlpage 提供的方法,但找不到方法。
我将不胜感激有关此的任何帮助。
谢谢。