WebDriver 中的选项选择

发布于 2024-11-29 09:12:59 字数 2196 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-12-06 09:12:59

我也有这个问题。尝试使用 Select 对象包装 WebElement

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

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

I had this problem too. Try wrapping the WebElement with a Select object:

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

public void setSelectedField(WebElement element, String value) {
    Select dropdown = new Select(element);
    dropdown.selectByVisibleText(value);
}
你怎么这么可爱啊 2024-12-06 09:12:59

伙计,这似乎很难相信,但一个月前我经常出现磁盘空间不足的情况,突然间所有测试都像这样失败了。从您粘贴的代码中可以看出,它显然没有失败的理由......

此外,我还看到您正在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文