我正在使用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;
}
}
}
}
也許你需要實現org.openqa.selenium.support.ui.WebDriverWait等待元素(正常加載) – 2011-08-12 09:48:00
我說,這是不是問題.. 。我已經試過 – lisak
它在該頁面處理10個其他字段,然後它進入選擇字段。我等待3秒鐘,這些字段加載完畢...我在1秒之前... PageFactory.initElements(new AjaxElementLocatorFactory(driver,3),this); – lisak