2016-10-12 3 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)". 이는 각 공항마다 고유하지만 텍스트의 고유 한 부분을 사용할 수 있습니다.

관련 문제