J. Doe
J. Doe

Reputation: 1529

Compound class names not permitted in Selenium

I ran into a problem when I was searching for a button to select. When I try to search for it using

        if (driver.getPageSource().contains("pager")) {
            WebElement pageSelector = driver.findElement(By.className("pager__item pager__item--last"));
            System.out.println("hello");
        pageSelector.click();
        }

However, it gives me the error - Compound class names not permitted. I read about hte issue, and it seems that using a csselector would be the way to solve it instead of using By.className. However, I cannot get the csSelector to work. This is the piece of html that contains the element I am searching for.

<div class="view-content">
<div class="item-list">
</div>
<h2 class="element-invisible">Pages</h2>
<ul class="pager">
<li class="pager__item pager__item--current">1</li>
<li class="pager__item">
<li class="pager__item">
<li class="pager__item">
<li class="pager__item">
<li class="pager__item">
<li class="pager__item pager__item--next">
<li class="pager__item pager__item--last">
<a href="/blogs?date_filter[value]&field_term_programs_offices_tid=All&page=5" title="Go to last page">last »</a>

How can I select the "pager__item pager__item--last" item?

Upvotes: 1

Views: 2345

Answers (1)

alecxe
alecxe

Reputation: 473873

You can just search for the pager__item--last class - it uniquely identifies the element, given the HTML code you have presented:

driver.findElement(By.className("pager__item--last"));

Or, with a CSS selector:

driver.findElement(By.cssSelector(".pager__item--last"));

Or, making an exact class match:

driver.findElement(By.cssSelector("li[class='pager__item pager__item--last']"));

Upvotes: 1

Related Questions