2016-08-07 9 views
0

Seleniumを新しくしました。Selenium Webdriverのボタンクリック()は、検索結果を返しません。

セレンについて詳しくは、JavaのSelenium Webドライバを使用して、ウェブサイト「https://www.findmyfare.com/」のフライト検索フローを自動化し始めました。

検索ボタンをクリックすることができました。しかし、私の検索条件の結果を生成するのではなく、エラーページを表示します。

検索を手動で実行すると、正常に動作します。

誰かがこの問題を解決するのに役立つでしょうか?

N.B.この検索ボタンは「送信」の一種ではありません。だから、私はclick()メソッドを使用しました。 (私もsubmit()で試しましたが、違いはありませんでした)。

以下は、検索をトリガーするコードセグメントです。

import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.Test; 

public class FlightSearchWD 
{ 
public WebDriver driver = null; 
public String baseUrl; 

@Test 
public void testFlightSearchWD() 
{ 

    driver = new FirefoxDriver(); 
    driver.get("https://www.findmyfare.com/"); 
    driver.findElement(By.id("from_1")).click(); 
    WebElement cleartext = driver.findElement(By.id("from_1")); 
    cleartext.clear(); 

    WebElement FromCity = driver.findElement(By.id("from_1")); 
    FromCity.sendKeys("Auckland, New Zealand (AKL)"); 

    driver.findElement(By.id("to_1")).click(); 
    WebElement cleartext1 = driver.findElement(By.id("to_1")); 
    cleartext1.clear(); 
    WebElement ToCity = driver.findElement(By.id("to_1")); 
    ToCity.sendKeys("Colombo, Sri Lanka (CMB)"); 
    WebElement FromDate = driver.findElement(By.id("date_1")); 
    FromDate.click(); 

    WebElement datepicker = driver.findElement(By.id("ui-datepicker-div")); 
    List<WebElement> rows = datepicker.findElements(By.tagName("tr")); 
    List<WebElement> columns = datepicker.findElements(By.tagName("td")); 

    for (WebElement cell : columns) 
    { 
     // Select 20th Date 
     if (cell.getText().equals("20")) 
     { 
      cell.findElement(By.linkText("20")).click(); 
      break; 
     } 
    } 
    // select to date 

    WebElement ToDate = driver.findElement(By.id("date_2")); 
    ToDate.click(); 

    WebElement datepicker2 = driver.findElement(By.id("ui-datepicker-div")); 
    List<WebElement> rows1 = datepicker2.findElements(By.tagName("tr")); 
    List<WebElement> columns1 = datepicker2.findElements(By.tagName("td")); 

    for (WebElement cell1 : columns1) 
    { 
     // Select 20th Date 
     if (cell1.getText().equals("25")) 
     { 
      cell1.findElement(By.linkText("25")).click(); 
      break; 
     } 
    } 

    WebElement ClickTravellers = driver.findElement(By.id("PopS")); 
    ClickTravellers.click(); 
    addAdults(); 
    addChildren(); 
    addInfant(); 

    WebElement clickSearch = driver.findElement(By.id("search_flight_submit")); 
    clickSearch.click(); 

} 

private void addAdults() 
{ 
    while (true) 
    { 
     WebElement popOverBtnGrp = driver 
       .findElement(By.xpath("//div[@class='btn-group col-xs-12 col-md-12 col-sm-12']")); 
     if (popOverBtnGrp.isDisplayed()) 
     { 
      try 
      { 
       WebElement NoOfTravellers = driver.findElement(By.xpath(
         "//button[@class='btn add_people ripple-effect btn-default btn-sm col-xs-2 col-md-4 col-sm-4']")); 
       for (int i = 0; i < 1; i++) 
       { 
        NoOfTravellers.click(); 
       } 
       break; 
      } catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

} 

private void addChildren() 
{ 
    while (true) 
    { 
     WebElement popOverBtnGrp = driver 
       .findElement(By.xpath("//div[@class='btn-group col-xs-12 col-md-12 col-sm-12']")); 
     if (popOverBtnGrp.isDisplayed()) 
     { 
      try 
      { 
       WebElement NoOfTravellers = driver.findElement(By.xpath("//button[@class=' add_people btn btn-default btn-sm ripple-effect col-xs-2 col-md-4 col-sm-4' and @data-id='childrens']")); 
       for (int i = 0; i < 1; i++) 
       { 
        NoOfTravellers.click(); 
       } 

       break; 
      } catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } 

     } 

    } 

} 


private void addInfant() 
{ 
    while (true) 
    { 
     WebElement popOverBtnGrp = driver 
       .findElement(By.xpath("//div[@class='btn-group col-xs-12 col-md-12 col-sm-12']")); 
     if (popOverBtnGrp.isDisplayed()) 
     { 
      try 
      { 
       WebElement NoOfTravellers = driver.findElement(By.xpath("//button[@class='add_people btn btn-default btn-sm ripple-effect col-xs-2 col-md-4 col-sm-4' and @data-id='infants']")); 
       for (int i = 0; i < 1; i++) 
       { 
        NoOfTravellers.click(); 
       } 

       break; 
      } catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } 

     } 

    } 

} 


} 
+2

質問にコードを追加してください – Guy

+0

検索をトリガするコードを追加しました。 –

+0

ボタンのクリックがエラーページにリダイレクトされているので、ボタンのクリックがうまくいきます。その他のエラーがあるはずです。検索コード全体を共有していただけますか? – Harish

答えて

0

ありがとう@Asha、私もselenium webdriverを使い始めていて、このクエリから多くのことを学び、最終的に解決策を見つけました。

import java.util.List; 

import org.testng.annotations.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.Keys; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class FlightSearchWD { 
public WebDriver driver = null; 
public String baseUrl; 

@Test 
public void testFlightSearchWD() throws InterruptedException { 
    driver = new FirefoxDriver(); 
    driver.get("https://www.findmyfare.com/"); 
    WebDriverWait wait = new WebDriverWait(driver, 10); 
    WebElement from1 = wait.until(ExpectedConditions 
      .presenceOfElementLocated(By.id("from_1"))); 
    from1.click(); 
    from1.clear(); 
    from1.sendKeys("AKL"); 
    from1.sendKeys(Keys.ENTER); 
    driver.findElement(By.id("to_1")).click(); 
    WebElement cleartext1 = driver.findElement(By.id("to_1")); 
    cleartext1.clear(); 
    WebElement ToCity = driver.findElement(By.id("to_1")); 
    ToCity.sendKeys("CMB"); 
    ToCity.sendKeys(Keys.ENTER); 
    Thread.sleep(3000); 
    driver.findElement(By.xpath(".//*[@id='trip_types']")).click(); 
    driver.findElement(By.id("date_1")).click(); 
    List<WebElement> total_date = driver 
      .findElements(By 
        .xpath("//div[@class='panel-body']/div[@class='pull-left col-md-6 pull-left-first']/table[1]//td")); 
    int total_date_size = total_date.size(); 
    for (int i = 0; i < total_date_size; i++) { 
     String date = total_date.get(i).getText(); 
     if (date.equalsIgnoreCase("20")) { 
      total_date.get(i).click(); 
      break; 
     } 
    } 
    driver.findElement(By.id("date_2")).click(); 
    List<WebElement> total_date1 = driver 
      .findElements(By 
        .xpath("//div[@class='panel-body']/div[@class='pull-left col-md-6 pull-left-first']/table[1]//td")); 
    int total_date_size1 = total_date1.size(); 
    for (int i = 0; i < total_date_size1; i++) { 
     String date1 = total_date1.get(i).getText(); 
     if (date1.equalsIgnoreCase("25")) { 
      total_date1.get(i).click(); 
      break; 
     } 
    } 
    WebElement ClickTravellers = driver.findElement(By.id("PopS")); 
    ClickTravellers.click(); 
    addAdults(); 
    addChildren(); 
    addInfant(); 

    WebElement clickSearch = driver.findElement(By 
      .id("search_flight_submit")); 
    clickSearch.click(); 

} 

private void addAdults() { 
    while (true) { 
     WebElement popOverBtnGrp = driver 
       .findElement(By 
         .xpath("//div[@class='btn-group col-xs-12 col-md-12 col-sm-12']")); 
     if (popOverBtnGrp.isDisplayed()) { 
      try { 
       WebElement NoOfTravellers = driver 
         .findElement(By 
           .xpath("//button[@class='btn add_people ripple-effect btn-default btn-sm col-xs-2 col-md-4 col-sm-4']")); 
       for (int i = 0; i < 1; i++) { 
        NoOfTravellers.click(); 
       } 
       break; 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

} 

private void addChildren() { 
    while (true) { 
     WebElement popOverBtnGrp = driver 
       .findElement(By 
         .xpath("//div[@class='btn-group col-xs-12 col-md-12 col-sm-12']")); 
     if (popOverBtnGrp.isDisplayed()) { 
      try { 
       WebElement NoOfTravellers = driver 
         .findElement(By 
           .xpath("//button[@class=' add_people btn btn-default btn-sm ripple-effect col-xs-2 col-md-4 col-sm-4' and @data-id='childrens']")); 
       for (int i = 0; i < 1; i++) { 
        NoOfTravellers.click(); 
       } 

       break; 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } 

     } 

    } 

} 

private void addInfant() { 
    while (true) { 
     WebElement popOverBtnGrp = driver 
       .findElement(By 
         .xpath("//div[@class='btn-group col-xs-12 col-md-12 col-sm-12']")); 
     if (popOverBtnGrp.isDisplayed()) { 
      try { 
       WebElement NoOfTravellers = driver 
         .findElement(By 
           .xpath("//button[@class='add_people btn btn-default btn-sm ripple-effect col-xs-2 col-md-4 col-sm-4' and @data-id='infants']")); 
       for (int i = 0; i < 1; i++) { 
        NoOfTravellers.click(); 
       } 

       break; 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } 

     } 

    } 

} 

} 

これに関する問題がある場合はお知らせください。

+0

これは私にとってもうまくいきます。 –

0

セレンは、ブラウザでのユーザー対話をエミュレートするのに非常に適しています。ただし、デフォルトではコマンドは単純な順序に従います。ドライバでページを読み込みます。ページの読み込みが完了すると、直ちに指示したアクションが実行されます。要素を見つけると言うと、それはByロケータに一致する要素を見つけるためにDOMを通過します。それが見つかると、それを返します。見つからない場合は、例外がスローされます。これはセレンプロジェクトや非常に静的なWebサイトのデフォルトとしては優れていますが、今日のWebサイトは動的です。ページは「読み込まれた」とみなされますが、ほとんどのコンテンツは非同期呼び出しによって取得されます。これは、「折りたたみの上にある」部分や重要な部分とのやり取りを許可し、気にしないセクションを待たせないようにすることで、ページが「うんざり」する感覚をユーザに与えるからです。残念なことに、これにより、最新のWebサイトを自動化することが難しくなります。一般に、非静的なWebサイトでSeleniumを使用してWebDriverWaitとのすべてのやりとりをラップして、要素をクリックしようとする前の状態にあることを確認するのが最適です。以下の条件を待つ簡単な例。

import org.junit.jupiter.api.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class FooTest { 

    @Test 
    public void foo() { 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("https://www.findmyfare.com/"); 
     new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("from_1"))) 
            .click(); 
     // ... etc 
     driver.quit(); 
    } 
} 

上記のWebドライバ待機のコンストラクタの番号「10」は、秒単位のタイムアウトです。 10秒以内に一致するIDを持つ要素がクリック可能な状態で見つからない場合、例外がスローされます。

0

他は、要素の呼び出しに待機が必要であると言って正しいです。あなたは何とかwhileループを実行することで回避することができましたが、待ちはほとんどいつも良いです。あなたのコードをダウンロードして実行しました。あなたが直面している問題は、実際にはセレンに直接関連するものではありません。

結果ページに行くのではなく、エラーページが表示され、それがウェブサイトの観点から期待される動作だと言われました。私の最高の推測は、彼らのウェブサイトをナビゲートしている「ユーザー」が「ボット」であるかどうかを検出し、「ボット」であれば、通常の検索結果ページの代わりに別のページに移動することです。あなたのセレンのスクリプトをテストするために別のウェブサイトを試してみてください。

とにかく、私は初心者向けのセレンフレームワークを作成し、ここで述べた問題を回避するのに役立ちます。テストが完了すると、自動的にブラウザを閉じます。簡単なフレームワークを使って上記のコードを翻訳したものです。

@Test 
public void testFindMyFare() throws Exception { 
    try(EasyTest easy = new EasyTest(DriverType.CHROME)) { 

     // start 
     easy.start("https://www.findmyfare.com/"); 

     // homepage 
     easy.newPage(page -> { 

      page.typeText("#from_1", "Auckland, New Zealand (AKL)"); 
      page.typeText("#to_1", "Colombo, Sri Lanka (CMB)"); 

      // click from date slector and select 20th 
      page.click("#date_1"); 
      page.executeIn("#ui-datepicker-div", container -> { 
       container.click(":20"); 
      }); 

      // click to date slector and select 25th 
      page.click("#date_2"); 
      page.executeIn("#ui-datepicker-div", container -> { 
       container.click(":25"); 
      }); 

      // click travellers 
      page.click("#PopS"); 

      // add people 
      page.executeIn(".popover", container -> { 
       container.click("button[data-id='adults'].add_people"); 
       container.click("button[data-id='childrens'].add_people"); 
       container.click("button[data-id='infants'].add_people"); 
      }); 

      // click find flight button 
      page.clickButton("#search_flight_submit"); 

     }); 

    } 
} 

あなたが読み、フォローしやすい上記のテストを見つけた場合、あなたはhttps://github.com/codezombies/easytestでeasytestフレームワークをダウンロードすることができます。とにかくセレンを学んで助けることができるかどうか教えてください。

+0

セレンIDEを使用して同じテストを行いましたが、正常に動作します。上記の答えに基づいて –

+0

、私は自分自身をテストし、テキストボックスでEnterキーを押す必要があります。私は何が起こっているのは、ENTERが押されていないと、テキストボックスの値が正しく設定されていないということです。 – sevenFLiP

関連する問題