mercredi 26 août 2015

How to directly find WebElements by their attributes except "class" and "name" (for example "title")

I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.

I use:

driverChrome.findElements(By.className("blabla"));

to find elements which have "blabla" as their className, for example:

<span class="blabla" title="the title">...</span>

Now, what if I want to find all elements by their other attributes? something like:

driverChrome.findElements(By.titleValue("the title"));

This is the code that I am currently using to do this task:

List<WebElement> spans = driverChrome.findElements(By.tagName("span"));

for (WebElement we : spans) {

    if (we.getAttribute("title") != null) {
            if (we.getAttribute("title").equals("the title")) {
                    ...
                    break;
            }
    }

}

but it is not fast and easy to use.

Aucun commentaire:

Enregistrer un commentaire