2017-04-18 45 views
0

我正在構建一個有兩個組合框的網站的蜘蛛,第一個組合框填充第二個。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()它是空的。

任何線索如何使它工作?

+0

請使用最新版本,並提供競爭案例(帶有URL)。 –

+0

Hi @AhmedAshour,剛剛添加了一個Junit和其中的URL。將很容易重現它。我使用的是2.26版本 –

回答

0

latest build,你可以得到options,但也有其他錯誤。

你可以通過.setJavaScriptEnabled(false)繞過他們。

兩個錯誤:

  1. TypeError: Cannot call method "post" of undefined (https://tutasawc.ucontactcloud.com/webchatclient/integrawebchat.js#91)

    ,並通過查看integrawebchat.jsendSession即使windowProxy沒有設置叫,因爲sessionStorage.getItem("integrachatstarted")計算結果爲false

    我不確定這是不是生產網站,而且這是一個有效的錯誤。


  • No permitted "Access-Control-Allow-Origin" header.

    同樣,我不知道這一個,並進一步調查是否應該做或不該。

  • +0

    謝謝@AhamedAshour, 但我需要JavaScriptEnabled爲true ...因爲如果JavaScriptEnable被禁用,則前一頁的按鈕不起作用。 我該怎麼辦?你知道爲什麼組合不能使用JavaScript嗎? 謝謝 –