2011-12-02 63 views
0

我試圖單擊combobox中的項目。該項目應該加載一個新的頁面。但瀏覽器只選擇該項目而不打開新頁面。這是片段:將無法​​在組合框上工作?

package org.example.tests; 

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 

import javax.swing.JOptionPane; 

import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class TestQuickLinks { 
    private WebDriver driver; 
    private String baseUrl="http://www.stts.edu"; 
    private StringBuffer verificationErrors = new StringBuffer(); 
    @Before 
    public void setUp() throws Exception { 
     driver = new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     driver.get(baseUrl); 
    } 

    @Test 
    public void testQuickLinks() throws Exception { 
     driver.findElement(By.id("link")).click(); 
     driver.findElement(By.xpath("//option[@value='Organisasi']")).click(); 
     Thread.sleep(5000); 
     try { 
      assertEquals("Organisasi STTS - Tentang STTS - Sekolah Tinggi Teknik Surabaya", driver.getTitle()); 
     } catch (Error e) { 
      verificationErrors.append(e.toString()); 
      JOptionPane.showMessageDialog(null, "this is not the correct page"); 
     } 
    } 
} 

我試圖手動對項目點擊,它工作正常。 我也嘗試在虛擬頁面上使用相同的代碼,它工作正常。誰能幫我? 我試過在mIRC上詢問,但他們忽略了我......

+0

當您提出涉及特定行爲的問題時,還應該發佈頁面的HTML。 –

+0

該頁面的HTML包含一個大的JavaScript,我不知道它的哪個部分處理我正在尋找的行爲...該片段有一個baseUrl的網站,雖然... – dapidmini

+0

你可以請提及的名稱組合頁面中的組合?只有你的selenium webdriver代碼不足以理解細節。你能否請你手動描述你點擊的步驟? –

回答

2

我建議您不要使用byXpath方法,而應嘗試使用By.linkText,如果它的鏈接或者您也可以使用By.id/按名字。

+0

該元素只具有「值」屬性。它的linkText也存在於頁面的不同部分,所以我不能使用linkText或name ..我想使用「value」屬性,但Selenium 2似乎沒有這種類型的標識符... – dapidmini

0

使用您的SelectElement類。我不確定這在Java中如何工作,但這裏是C#方法:

public class TestQuickLinks { 
    private WebDriver driver; 
    private String baseUrl="http://www.stts.edu"; 
    private StringBuffer verificationErrors = new StringBuffer(); 
    @Before 
    public void setUp() throws Exception { 
     driver = new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     driver.get(baseUrl); 
    } 

    @Test 
    public void testQuickLinks() throws Exception { 
     WebElement thisElement =driver.FindElement(By.CssSelector("select[id='link']")); 
     SelectElement select = new SelectElement(thisElement); 
     select.SelectByIndex(3); //sets the combo box or select box to the desired value 
     thisElement.Click(); //this is needed to activate the sites javascript onChange event. 
    }