click命令对组合框不起作用?

时间:2011-12-02 11:05:25

标签: selenium combobox click

我正在尝试点击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,但是他们忽略了我......

2 个答案:

答案 0 :(得分:2)

我建议你不要使用byXpath方法,而是尝试使用By.linkText(如果它是一个链接,或者你也可以使用By.id/By.name。

答案 1 :(得分: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.
    }