2012-02-20 37 views
0

我試圖與硒的演練用於搜索的特價機票,在網站上。我已經能夠完成所有的搜索過程,但現在我被選中的航班卡住了。我設法按公司名稱訂購航班。現在我需要點擊兩個首航(出發,到達)。硒 - 點擊自動生成隨機DIV/ID/LINK

正如你所附加的圖像上看到,網頁生成兩個表。他們每個人都列出了一些航班。我需要點擊每個表格的第一個。

的問題是,生成的列表使用不同的DIV的ID爲不同的公司,而且標識具有隨機數(「_X」,X是一個隨機數)。

我只需要點擊每個表的DIV的,任何comlumn應該做到這一點。但是所有列都使用相同的名稱(在兩個表上)。

使用Selenium IDE現在的工作,但僅僅幾個小時,因爲航班,價格,時間不斷更新和網頁顯示每個時間,這意味着不同的ID,每次不同的結果。

有沒有辦法解決這個問題?

重要提示:我使用JAVA硒。沒有PERL,沒有PYTHON或其他。

這將是現在使用JAVA的步驟:

// This orders the departure flight by company. 
selenium.click("id=orden-compania-ida"); 
selenium.click("id=orden-compania-ida"); 

// This orders the arrival flights by company. 
selenium.click("id=orden-compania-vuelta"); 
selenium.click("id=orden-compania-vuelta"); 

// This would click on the first link of first table 
selenium.click("css=div.col-3 > label"); 
selenium.click("id=I_5"); 

// This would click on the first link of the second table 
selenium.click("css=#TV_GDSAMADEUS_7 > div.col-3 > label"); 
selenium.click("id=V_12"); 

此時這個工作,但飛行更新後,這將不再工作。有沒有什麼辦法可以讓硒點擊每張桌子右側的第一次飛行?

非常感謝。

LINK TO IMAGE

回答

0

在我看來,你應該儘量選擇通過CSS選擇器而不是使用那些ID必要資料覈實。請仔細閱讀以下代碼,以明確我的意思。

// Open Firefox driver. 
    WebDriver driver = new FirefoxDriver(); 

    // Send a get request. 
    driver.get("http://google.com"); 

    // Typing a search query. 
    WebElement searchField = driver.findElement(By.name("q")); 
    searchField.sendKeys("Cheese!"); 

    // Waiting for the driver to change its title and load search results. 
    new WebDriverWait(driver, 20).until(new ExpectedCondition<Boolean>() { 
     public Boolean apply(WebDriver d) { 
      return d.getTitle().toLowerCase().contains("cheese"); 
     } 
    }); 

    // Obtain dymamically loaded search results. 
    List<WebElement> results = driver.findElements(By.cssSelector("h3.r")); 

    // Go through the search results 
    for (WebElement element : results) { 
     // You can click instead of printing. 
     System.out.println(element.getText()); 
    } 

    // Close the driver. 
    driver.close(); 

這是一個edited example from the Official Selenium site

+0

問題是我使用的是Selenium RC,而不是webdriver。並且不支持「driver.findElements」... – 2012-02-21 08:00:08