2016-04-14 38 views
0

我使用Selenium和Java編寫測試。某處DOM,我們有這樣的選擇:元素是從下拉菜單中選擇的,但不是來自jQuery

<select name="operator" id="jsonform-20577-elt-operator" required=""> 
    <option value="<"> < </option> 
    <option value="<="> <= </option> 
    <option value="="> = </option> 
    <option value=">"> > </option> 
    <option value=">="> >= </option> 
</select> 

當我使用:

WebElement element= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
        "//select[@name='operator']"))); 

action.sendKeys(element, "<=").build().perform(); 

它選擇< =從下拉菜單中,但是當我們檢查從jQuery的選擇值,我們可以看到,它沒有改變(它有以前的值)。

,但如果我添加keys.ENTER

action.sendKeys(element, "<=",keys.ENTER).build().perform(); 

它的工作原理。爲什麼沒有進入就沒有工作?爲什麼從下拉框中選擇,但後來在應用程序的其他部分它仍然有以前的價值?

+0

那麼問題是什麼? – Guy

+0

爲什麼它沒有進入工作?爲什麼從下拉框中選擇,但後來在應用程序的其他部分它仍然有以前的價值? –

回答

0

使用sendKeys()來下拉不選擇選項,只鍵入文字。硒具有Select類來處理的下拉列表中選擇

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@name='operator']"))); 
Select select = new Select(element); 

// select by text 
select.selectByVisibleText("<="); 
// or select by value 
select.selectByValue("<="); 

注意,這將在<select>標籤纔有效。

相關問題