首页 > 解决方案 > InvalidSelectorException:无效选择器:不允许复合类名

问题描述

我尝试单击选择菜单并选择元素:

<div id="_desktop_currency_selector">
    <div class="currency-selector dropdown js-dropdown">
        <span>Currency:</span>
        <span class="expand-more _gray-darker hidden-sm-down" data-toggle="dropdown" aria-expanded="false">UAH ₴&lt;/span>
        <a data-target="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="hidden-sm-down">
            <i class="material-icons expand-more">&lt;/i>
        </a>
        <ul class="dropdown-menu hidden-sm-down" aria-labelledby="dLabel" style="display: none;">
            <li>
                <a title="EUR" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=2" class="dropdown-item">EUR €&lt;/a>
            </li>
            <li class="current">
                <a title="UAH" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=1" class="dropdown-item">UAH ₴&lt;/a>
            </li>
            <li>
                <a title="USD" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=3" class="dropdown-item">USD $</a>
            </li>
        </ul>
        <select class="link hidden-md-up">
            <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=2">EUR €&lt;/option>
            <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=1" selected="selected">UAH ₴&lt;/option>
            <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=3">USD $</option>
        </select>
    </div>
</div>

我的方式:

WebElement element1 = driver.findElement(By.className("link hidden-md-up"));
Select dropList = new Select(element1);
//    debug sysout
dropList.getOptions().forEach(p -> System.out.println(p.getText()));

结果我得到了这个异常:

org.openqa.selenium.InvalidSelectorException:无效选择器:不允许复合类名


如何正确点击一个元素,使用selenium

标签: javaselenium

解决方案


例外是由于选择器中使用了多个类。更改选择器以使用单个类或 cssSelector。请参见下面的示例。检查这些选择器是否返回唯一(必需)元素。

WebElement element1 = driver.findElement(By.className("hidden-md-up"));

或者

WebElement element1 = driver.findElement(By.cssSelector(".link.hidden-md-up"));

推荐阅读