我正在構建一個有兩個組合框的網站的蜘蛛,第一個組合框填充第二個。HtmlUnit HtmlSelect不觸發事件
這它與一個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()
它是空的。
任何線索如何使它工作?
請使用最新版本,並提供競爭案例(帶有URL)。 –
Hi @AhmedAshour,剛剛添加了一個Junit和其中的URL。將很容易重現它。我使用的是2.26版本 –