【问题标题】:InvalidSelectorException: invalid selector: Compound class names not permittedInvalidSelectorException:无效选择器:不允许复合类名
【发布时间】:2019-12-08 07:03:03
【问题描述】:

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

<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:无效选择器: 不允许复合类名


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

【问题讨论】:

    标签: java selenium


    【解决方案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
    

    【讨论】:

    • 我得到一个新异常(在driver.findElement.... 行):org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//div[@class='currency-selector dropdown js-dropdown open']/a[@data-toggle='dropdown']"}
    • 检查定位器,我改变了定位器,我错误地给了定位器打开下拉它应该是下拉箭头它现在可以工作,如果你注意到的话,我从类名中删除了单词open,在点击之前使用一些睡眠/等待
    【解决方案2】:

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

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

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

    【讨论】:

    • 不幸的是,在上述情况下,我得到了超过 1 个元素。还有其他解决办法吗?
    • 您是否有超过 1 个带有该定位器的选择元素?根据给出的 html,这应该可以工作 - By.cssSelector("select.link.hidden-md-up")
    • 结果中有 2 个元素。此外,我在Select 实例中没有结果:`Select dropList = new Select(element.get(0)); String s = dropList.getFirstSelectedOption().getText();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2015-04-15
    • 2017-04-22
    • 1970-01-01
    • 2020-11-04
    • 2015-11-09
    相关资源
    最近更新 更多