2011-08-12 59 views
0

我正在使用WebDriver和selenium-firefox-driver版本2.3.1。現在,當棄用option.setSelected();,必須直接或者更確切地說做option.click();WebDriver中的選項選擇

if (value.equals(option.getAttribute("value"))) { 
    if(!option.isSelected()) { 
     option.click(); 
     break; 
    } 
} 

的問題是,我得到這個異常沒有道理。

元素是不可見的,所以可能無法與

<select id="deadLineDay" name="deadLineDay"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
</select> 

而且進行交互,這絕不是一個時間問題......任何想法到底是什麼?拋出異常只是有時,但就像我說的,不是一個時機的問題,我調試

這是代碼:

public FillOutForm(WebDriver driver, UploadDocumentPage parent) { 
    this.driver = driver; 
    this.parent = parent; 
    PageFactory.initElements(new AjaxElementLocatorFactory(driver, 3), this); 
} 

@FindBy(how = How.NAME, using = day) 
private WebElement deadLineDay; 
@CacheLookup 
@FindBy(how = How.NAME, using = hour) 
private WebElement deadLineHour; 
@CacheLookup 
@FindBy(how = How.NAME, using = minute) 
private WebElement deadLineMinute; 
@CacheLookup 
@FindBy(how = How.NAME, using = AmPm) 
private WebElement deadLineAmPm; 
@CacheLookup 
@FindBy(how = How.ID, using = desc) 
private WebElement description; 
@CacheLookup 
@FindBy(how = How.ID, using = comm) 
private WebElement comment; 

public boolean validationPasses(Map<String, String> map) { 

    try { 
     for (String key : map.keySet()) { 
      WebElement we = (WebElement) this.getClass().getDeclaredField(key).get(this); 
      setSelectedField(we, map.get(key)); 
     } 
    } catch (Exception e) { 
     throw new Error(e.getMessage()); 
    } 

    valid = elementExists(driver, By.className(validatorError)); 

    return valid; 
} 

public void setSelectedField(WebElement element, String value) { 
    List<WebElement> options = element.findElements(By.tagName("option")); 
    for (WebElement option : options) { 
     if (value.equals(option.getAttribute("value"))) { 
      if(!option.isSelected()) { 
       option.click(); 
       break; 
      } 
     } 
    } 
} 
+0

也許你需要實現org.openqa.selenium.support.ui.WebDriverWait等待元素(正常加載) – 2011-08-12 09:48:00

+0

我說,這是不是問題.. 。我已經試過 – lisak

+0

它在該頁面處理10個其他字段,然後它進入選擇字段。我等待3秒鐘,這些字段加載完畢...我在1秒之前... PageFactory.initElements(new AjaxElementLocatorFactory(driver,3),this); – lisak

回答

-1

男子它似乎很難相信,但一個月前我經常在盤面上出現空間,突然間所有的測試都失敗了。它顯然沒有理由失敗,只要我可以看到你粘貼的代碼...

另外我看到你正在使用AjaxElementLocatorFactory。切換到DefaultElementLocatorFactory,它可能會消失。

0

我也有這個問題。嘗試用Select對象包裹WebElement

import org.openqa.selenium.support.ui.Select; 
... 

public void setSelectedField(WebElement element, String value) { 
    Select dropdown = new Select(element); 
    dropdown.selectByVisibleText(value); 
} 
+0

Joe這真的是最令人厭惡的bug之一。由於埃德加回答我再次運行測試。它回來了!包裝到Select對象中並沒有幫助。實際上,當我包裝它時,根本找不到它。\ – lisak

+0

這種方式最終在option.click();以及它幾乎是一樣的,方便.... – lisak

+0

頁面是靜態的,還是你在做一些Ajaxy?我懷疑競賽狀況。如果您發佈您的標記和附帶的ECMAScript,我會仔細研究一下。 –

相關問題