HtmlUnit HtmlSelect不会触发事件

时间:2017-04-18 22:01:52

标签: java htmlunit

我正在为一个有两个组合框的网站构建一个蜘蛛,第一个填充第二个组合框。

这是通过javascript完成的:

  <script>var initAutocomplete2Flag = 1;

    $(document).ready(function () {


        if ($('#gender').val() == 'Female') {

            marital_options = '';
            var selected = '';
            $.each(marital_status_female, function (index, value) {
                if (value == 1) {
                    selected = 'selected="selected"';
                } else {
                    selected = '';
                }
                marital_options += '<option ' + selected + ' value="' + index + '">' + value + '</option>';
            });
            $('#marital_status').html('<select class="form-control" id="marital_status" name="marital_status">' + marital_options + '</select>');

        } else {
            marital_options = '';
            var selected = '';
            $.each(marital_status_male, function (index, value) {
                if (value == 1) {
                    selected = 'selected="selected"';
                } else {
                    selected = '';
                }
                marital_options += '<option ' + selected + ' value="' + index + '">' + value + '</option>';
            });
            $('#marital_status').html('<select class="form-control" id="marital_status" name="marital_status">' + marital_options + '</select>');
        }

        $('#gender').change(function () {

            if ($('#gender').val() == 'Female') {

                marital_options = '';
                var selected = '';
                $.each(marital_status_female, function (index, value) {
                    if (value == 1) {
                        selected = 'selected="selected"';
                    } else {
                        selected = '';
                    }
                    marital_options += '<option ' + selected + ' value="' + index + '">' + value + '</option>';
                });
                $('#marital_status').html('<select class="form-control" id="marital_status" name="marital_status">' + marital_options + '</select>');

            } else {
                marital_options = '';
                var selected = '';
                $.each(marital_status_male, function (index, value) {
                    if (value == 1) {
                        selected = 'selected="selected"';
                    } else {
                        selected = '';
                    }
                    marital_options += '<option ' + selected + ' value="' + index + '">' + value + '</option>';
                });
                $('#marital_status').html('<select class="form-control" id="marital_status" name="marital_status">' + marital_options + '</select>');
            }

        });

    });
</script>

这是我的代码

import org.junit.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;

public class BugTest {

    @Test
    public void test() throws Exception {

        try (final WebClient webClient = new WebClient(BrowserVersion.getDefault())) {

            webClient.getOptions().setThrowExceptionOnScriptError(true);
            webClient.getOptions().setJavaScriptEnabled(true);      
            webClient.getOptions().setUseInsecureSSL(true);

            final HtmlPage page = webClient.getPage("http://54.233.181.233/untitle.html");

            final HtmlSelect gender = page.getFirstByXPath("//*[@id=\"gender\"]");
            gender.setSelectedAttribute(gender.getOptionByValue("Male"),Boolean.TRUE);
            gender.setSelectedAttribute(gender.getOptionByValue("Female"),Boolean.TRUE);
            final HtmlSelect education = page.getFirstByXPath("//*[@id=\"education\"]");

            final HtmlSelect marital_status = page.getFirstByXPath("//*[@id=\"marital_status\"]");
            HtmlOption option = marital_status.getOptionByText("Other");
            marital_status.setSelectedAttribute(option,Boolean.TRUE);
        }
    }
}

问题在于marital_status.getOptions()是空的。

有任何线索如何使其发挥作用?

1 个答案:

答案 0 :(得分:0)

使用latest build,您可以获得options,但还有其他错误。

您可以通过.setJavaScriptEnabled(false)来解决这些问题。

这两个错误:

  1.   

    TypeError:无法调用方法&#34; post&#34;未定义的(https://tutasawc.ucontactcloud.com/webchatclient/integrawebchat.js#91

    并查看integrawebchat.jsendSession即使未设置windowProxy也会被调用,因为sessionStorage.getItem("integrachatstarted")评估为false

    我不确定这是否是一个制作网站,这是一个有效的错误。


    1.   

      不允许&#34;访问控制 - 允许 - 来源&#34;报头中。

      同样,我不确定这一点,是否应该进行进一步的调查。