java - Android 的 HtmlUnit 替代品?

标签 java android html selenium htmlunit

允许我填写具有复选框和单选按钮的 HTML 表单的替代方法。

我正在创建这个 android 应用程序,它要求用户输入并将该数据发送到带有 html 表单的网站,填写它,提交表单,并返回以下结果页面。

我已经设法将数据发送到 html 表单并使用 eclipse 中的 HtmlUnit 库检索页面(我已经在下面发布了 Java 代码)。

但是,当我将该代码复制到我的 Android 项目时,我发现 Android 不支持 HtmlUnit 库。

对于 Android,是否有其他替代 HtmlUnit 的方法?替代方案应该能够将文本、复选框、单选按钮填写到 Html 表单中,然后单击提交按钮

HTML 表单代码:

<form method="post" action="https://www.xxxxx.com/cgi-bin/xxxxxx.cgi">


<p><em>Person:</em>
<input size="18" name="name"><br>
<input type="radio" name="login" value="no" checked="">Name <input     type="radio" name="login" value="yes">Username</p>

<p><em>Title:</em>
<input size="18" name="title"></p>

<p><em>Department:</em>
<input size="18" name="department"></p>

<p><em>Groups to Search:</em><br>
<input type="checkbox" name="get_student" value="yes" checked=""> Students<br>
<input type="checkbox" name="get_alum" value="yes" checked=""> Alumni<br>

<input type="checkbox" name="get_staff" value="yes" checked=""> Staff<br>
<input type="checkbox" name="get_faculty" value="yes" checked=""> Faculty</p>

<p><input type="submit" value="Search"></p>
</form>

HtmlUnit Java 代码:

public static String submittingForm() throws Exception {


    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 

    WebRequest request = new WebRequest(new URL("https://www.xxxxx.com/"));

    // Get the first page
    HtmlPage page1 = webClient.getPage(request);

    System.out.println("PULLING LINKS/ LOADING:");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    List<HtmlForm> listform = page1.getForms();
    HtmlForm form = listform.get(0);


    HtmlElement Name = page1.getElementByName("name");
    Name.click();
    Name.type("Adonay");
    HtmlElement nameRadio = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='radio' and @value='no']");
    HtmlElement userRadio = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='radio' and @value='yes']");
   /* userRadio.click(); click when username wanted*/
    HtmlElement Title = page1.getElementByName("title");
    Title.click();
    Title.type("");
    HtmlElement Department = page1.getElementByName("department");
    Department.click();
    Department.type("");
    HtmlElement studentBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_student']");
    studentBox.click();
    //add clicker here
    HtmlElement alumniBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_alum']");
    alumniBox.click();
    //add clicker here
    HtmlElement staffBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_staff']");
    staffBox.click();
    //add clicker here
    HtmlElement facultyBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_faculty']");
    facultyBox.click();
    //add clicker here



    HtmlElement button = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='submit' and @value='Search']");
    // Change the value of the text field

    // Now submit the form by clicking the button and get back the second page.
    HtmlPage page2 = button.click();
    webClient.waitForBackgroundJavaScript(200);
    return(page2.asXml());
}

最佳答案

伙计们,我找到了另一种方法。因为我知道服务器地址,所以我尝试使用 DataOutputStream 将数据直接发布到它。看下面的代码:

public String searchDirectory() throws IOException {
    URL url = new URL("https://www.xxxxx.com/xxxxx/xxxxx.cgi");
    URLConnection con = url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream printout = new DataOutputStream (con.getOutputStream ());

    String parameters =("name=" + URLEncoder.encode("DATA HERE", "UTF-8"));
    printout.writeBytes (parameters);
    printout.flush ();
    printout.close ();
        /*InputStream inputStream = getApplicationContext().getResources().getAssets().open("White Pages Query Results.html");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        */
    DataInputStream input = new DataInputStream (con.getInputStream ());
    String line;
    String htmlCode = "";
    while((line = input.readLine()) != null) {
        htmlCode += line;
    }
    return htmlCode;
}

如您所见,我使用 Html 元素的名称通过 java 访问它们。但是,正如您从原始帖子中的 html 表单代码中看到的那样,单选按钮具有相同的名称,我如何在不使用它们的名称的情况下单独访问/填写它们?

关于java - Android 的 HtmlUnit 替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220828/

相关文章:

Java系统Properties,http.proxyHost,两个问题

java - 使用线程中断来唤醒线程?

java - 有没有办法在 Eclipse 中创建自定义警告?

java - 性能:我应该避免构造函数委托(delegate)吗?

Android,存储等待网络连接的http请求的解决方案/库

android - 在我所有的应用程序中显示 View

java - Android Studio : Early Access Java versions may cause compatibility issues

javascript - CSS 显示 :none for responsive web design

css - 表单按钮的 div 和 span css 样式

javascript - jQuery:向表中添加行并在 DOM 中选择它