tomasfly123
tomasfly123

Reputation: 25

Focus on an element Selenium

I have a screen that contains a set of filters which are displayed when pressing a specific key. I need to automate the selection of these filters.

Each filter has an Xpath which points to an html element that contains a text. I have tried the following:

private List<WebElement> elements;

public void reloadFilters() {
    String FilterXPath = this.FILTER_ITEM_XPATH;
    elements = webDriver.findElements(By.xpath(FilterXPath));   
}

and then

public void selectFilter(String filter) {
    WebElement W;
    Iterator I = elements.iterator();
    if (I.hasNext()) {
        W = (WebElement)I;
        if (W.getText().equals(filter)) {
            new Actions(webDriver).moveToElement(W).perform();
        }
    }
}

But I when I run this code I get:

java.lang.ClassCastException: java.util.ArrayList$Itr cannot be cast to org.openqa.selenium.WebElement

Any suggestion on how can I perform this?

Upvotes: 0

Views: 2430

Answers (1)

Qwerky
Qwerky

Reputation: 18455

Change;

W=(WebElement)I;

to

W=(WebElement)I.next();

also, use proper variable names;

public void selectFilter(String filter) {
    Iterator iter = elements.iterator();
    if (iter.hasNext()) {
        WebElement element = (WebElement)iter.next();
        if (element.getText().equals(filter))
        {
            new Actions(webDriver).moveToElement(element).perform();
        }
    }

}

Upvotes: 1

Related Questions