无法使用Selenium 2选择元素?

时间:2013-10-18 12:09:53

标签: java selenium

public void Test003() {
    new Select (webDriver.findElement(By.cssSelector("select"))).selectByVisibleText("NEC_COCONA_GG3");

    webDriver.manage().timeouts().implicitlyWait(1,TimeUnit.MILLISECONDS); 
    webDriver.findElement(By.xpath("//div[@id='main-menu']/ul/li[5]/a")).click();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    webDriver.findElement(By.id("issue_subject")).sendKeys("Test");
    webDriver.findElement(By.id("issue_description")).sendKeys("Test");
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    new Select(webDriver.findElement(By.id("issue_assigned_to_id"))).selectByVisibleText("<<me>>");
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    new Select(webDriver.findElement(By.id("issue_custom_field_values_7"))).selectByVisibleText("QA");
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    webDriver.findElement(By.id("issue_custom_field_values_27")).sendKeys("Test");
    webDriver.findElement(By.xpath("//*[@id='attachments_fields']/span/input[1]")).sendKeys("D:\\NEC new\\log\\EASCrash.txt");
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    webDriver.findElement(By.xpath("//*[@id='attachments_form']/span[2]/a")).click();
    webDriver.findElement(By.xpath("//*[@id='attachments_fields']/span[2]/input[1]")).sendKeys("D:\\NEC new\\log\\crash_info_201304171712.txt");
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    webDriver.findElement(By.xpath("//input[@name='issue[watcher_user_ids][]' and @value='102']")).click();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
    webDriver.findElement(By.xpath("//*[@id='issue-form']/input[1]")).click();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MILLISECONDS); 
}

在运行脚本时,selenium无法获取

的值
new Select (webDriver.findElement(By.cssSelector("select"))).selectByVisibleText("NEC_COCONA_GG3");

选择失败的测试用例。

显示的错误是:

org.openqa.selenium.WebDriverException: Driver does not support finding an element by selector: select

2 个答案:

答案 0 :(得分:0)

我认为您的问题可能源于多次选择。是否有任何属性可以唯一标识选择框?如果是这样,那么你需要唯一地识别它。如果有名称,请执行

By.cssSelector("select[name='someName']");

标题甚至可以使用。

By.cssSelector("select[title^='Select your']");

如果您心存开阔,请尝试this framework。 (您可以下载here

您的测试将重构为:

@Config(url="http://systemunder.test", browser=Browsers.CHROME)
public class MyTest extends AutomationTest {
    @Test
    public void Test003() {
        selectOptionByText (By.cssSelector("select"))
        .click             (By.cssSelector("div#main-menu > ul > li:nth-child(5) > a"))
        .setText           (By.id("issue_subject"), "Test")
        .setText           (By.id("issue_description"), "Test")
        .selectOptionByText(By.id("issue_assigned_to_id"), "<<me>>")
        .selectOptionByText(By.id("issue_custom_field_values_7"), "QA")
        .setText           (By.id("issue_custom_field_values_27"), "Test")
        .setText           (By.cssSelector("*#attachments_fields > span > input:nth-child(1)"), "D:\\NEC new\\log\\EASCrash.txt")
        .click             (By.cssSelector("*#attachments_form > span:nth-child(2) > a"))
        .setText           (By.cssSelector("*#attachments_fields > span:nth-child(2) > input"), "D:\\NEC new\\log\\crash_info_201304171712.txt")
        .click             (By.cssSelector("input[name='issue[watcher_user_ids][]'][value='102]"))
        .click             (By.cssSelector("form#issue-form > input:nth-child(1)"));
    }
}

您不必担心这样的隐式等待......它全部由框架处理。

答案 1 :(得分:-1)

请改为尝试:

webDriver.findElement(By.xpath("//select"))

似乎CSS选择器存在问题,但使用XPath应该可以。