2016-10-12 6 views
1

私はこのページのドロップダウンからテキストを選択するためのフェールセーフ(ish)メソッドを私に提供できますか?JavascrtptExecutorを使用してドロップダウンからオプションを選択できません

https://www.club18-30.com/club18-30

具体的

、空港ドロップダウン 'に' 'から' と。選択を登録していない

public void selectWhereFrom(String query, String whereFromSelect) throws InterruptedException { 
    WebElement dropDownContainer = driver.findElement(By.xpath(departureAirportLocator)); 
    dropDownContainer.click(); 

    selectOption(query,whereFromSelect); 
} 

public void selectOption(String query, String option) { 
    String script = 
      "function selectOption(s) {\r\n" + 
        " var sel = document.querySelector(' " + query + "');\r\n" + 
        " for (var i = 0; i < sel.options.length; i++)\r\n" + 
        " {\r\n" + 
        "  if (sel.options[i].text.indexOf(s) > -1)\r\n" + 
        "  {\r\n" + 
        "   sel.options[i].selected = true;\r\n" + 
        "   break;\r\n" + 
        "  }\r\n" + 
        " }\r\n" + 
        "}\r\n" + 
        "return selectOption('" + option + "');"; 

    javaScriptExecutor(script); 
} 

これが正常にテキストとボックスを移入するようだが、私がヒットしたときにそれを示唆し、私はその後、私はオプションを選択する必要があるというメッセージを受け取る「検索」:私は、次のコードを使用しています?

私はむしろJavaScriptExecutorを避けるだろうが、これらの選択は、通常のセレンの選択機構を動作させることができていない

答えて

0

私は出発空港を設定するための各ドロップダウン、いずれかの機能を設定し、設定するための別の考え目的地の空港。私は下のコードをテストして、それが動作します。

機能

public static void setDepartureAirport(String airport) 
{ 
    driver.findElement(By.cssSelector("div.departureAirport div.departurePoint")).click(); 
    String xpath = "//div[contains(@class, 'departurePoint')]//ul//li[contains(@class, 'custom-select-option') and contains(text(), '" 
      + airport + "')]"; 
    driver.findElement(By.xpath(xpath)).click(); 
} 

public static void setDestinationAirport(String airport) 
{ 
    driver.findElement(By.cssSelector("div.destinationAirport div.airportSelect")).click(); 
    String xpath = "//div[contains(@class, 'destinationAirport')]//ul//li[contains(@class, 'custom-select-option') and contains(text(), '" 
      + airport + "')]"; 
    driver.findElement(By.xpath(xpath)).click(); 
} 

、あなたは、私は、例えば、あなたがあなたの検索のための3文字の空港コードを使用することを示唆している

driver.get("https://www.club18-30.com/club18-30"); 
setDepartureAirport("(MAN)"); 
setDestinationAirport("(IBZ)"); 

のようにそれらを呼び出しますマンチェスターの "(MAN)"それは各空港に固有のものになりますが、テキストのユニークな部分を使用することができます。

関連する問題