2013-10-18 84 views
0
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 2選擇元素?

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 
+0

。您是否想要打開下拉菜單並選擇其中的一個項目? –

+0

另外,什麼是「select」? –

+0

您不必在每次查找元素時設置隱式等待,只需設置一次在開始時忘記它,它會完成它的工作,而且3毫秒可以忽略不計,並且可能不會幫助。 –

回答

-1

試試這個:

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

,似乎有與CSS選擇器的問題,但使用XPath應該工作。

+0

他沒有使用類名,他使用標籤名稱。在這兩種情況下'By.className()'和'By.tagName()'都是更好的選擇。無論如何,奇怪的是CSS選擇器不起作用。你有沒有看到任何瀏覽器拒絕他們? –

+0

好點@Slanec,我修改了我的例子來表示這個。 「className()」和「tagName()」是否更好是可以討論的;就我個人而言,我喜歡XPath,因爲它非常靈活,比CSS更重要。 – blalasaadri

+0

不要**推薦xpath over CSS。你可以用xpath完成什麼,你可以用CSS完成......更不用說CSS更快,更乾淨,而且更好。另外,xpath:'// select'和CSS:'select'等同於相同的東西。 – sircapsalot

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)")); 
    } 
} 

你會不會擔心這樣的隱式等待..這一切都由框架負責