有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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 ₴</span>
        <a data-target="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="hidden-sm-down">
            <i class="material-icons expand-more"></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 €</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 ₴</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 €</option>
            <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=1" selected="selected">UAH ₴</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: invalid selector: Compound class names not permitted


如何使用selenium正确单击元素


共 (2) 个答案

  1. # 1 楼答案

    它不适用于select类,因为它不是纯下拉列表,而是未排序的货币列表

    你需要通过点击打开下拉列表,这样做

    driver.driver.findElement(By.xpath(".//div[@class='currency-selector dropdown js-dropdown']/a[@data-toggle='dropdown']")).click(); 
    

    这个下拉列表将打开,现在使用xpath从列表中获取单个元素-

    .//a[@title='USD']   or  .//a[@title='USD']/parent::li
    
  2. # 2 楼答案

    该异常是由于选择器中使用了多个类造成的。将选择器更改为使用单个类或CSS选择器。见下面的例子。 检查这些选择器是否返回唯一(必需)元素

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

    或者

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