Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium CSS selector :visible is not a valid selector

I'm using Page Objects to map elements in a page, something like that:

public class MyPage {

    protected WebDriver driver;

    @FindBy(css = "a[data-code=panel]:visible")
    private WebElement cpaneladmin;

    public MyPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(this.driver, this);
    }
}

The problem is this :visible CSS selector. Aparently, Selenium does not support it. Is there a way to select only visible elements using xpath or another kind of CSS selector?

Thanks

like image 497
caarlos0 Avatar asked Nov 22 '13 18:11

caarlos0


1 Answers

@FindBy(css = "a[data-code=panel]")
private List<WebElement> cpaneladmin;

Then iterate through the elements until you find the one that is displayed.

public WebElement FindDisplayed(WebElements elements)
{
    foreach (WebElement element in elements)
    {
        if (element.isDisplayed()) // correct method: isDisplayed()
            return element;
    }
}
like image 52
Richard Avatar answered Oct 06 '22 00:10

Richard