WebDriver 中的选项选择
我正在使用 WebDriver 和 selenium-firefox-driver 版本 2.3.1。现在,当 option.setSelected();
被弃用时,必须直接执行 option.click();
或更准确地说:
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;
}
}
}
}
I'm using WebDriver and selenium-firefox-driver version 2.3.1. Now when option.setSelected();
deprecated, one must do option.click();
directly or more exactly :
if (value.equals(option.getAttribute("value"))) {
if(!option.isSelected()) {
option.click();
break;
}
}
The problem is, that I get this exception without reason.
Element is not currently visible and so may not be interacted with
<select id="deadLineDay" name="deadLineDay">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Also, it is definitely not a timing issue... Any idea what the hell is that ? The exception is thrown only sometimes, but as I say, not a timing issue, I'm debugging that
This is the code :
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;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有这个问题。尝试使用
Select
对象包装WebElement
:I had this problem too. Try wrapping the
WebElement
with aSelect
object:伙计,这似乎很难相信,但一个月前我经常出现磁盘空间不足的情况,突然间所有测试都像这样失败了。从您粘贴的代码中可以看出,它显然没有失败的理由......
此外,我还看到您正在使用 AjaxElementLocatorFactory。切换到 DefaultElementLocatorFactory,它可能会消失。
Man it might seem hard to believe, but a month ago I often got out of space on disk and suddenly all tests were failing like this. It obviously doesn't have a reason to fail as far as I can see from the code you pasted...
Also I see you're using AjaxElementLocatorFactory. Switch to DefaultElementLocatorFactory, it might go away.