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&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&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&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&id_currency=2">EUR €</option>
<option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=1" selected="selected">UAH ₴</option>
<option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&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
?
解决方案
例外是由于选择器中使用了多个类。更改选择器以使用单个类或 cssSelector。请参见下面的示例。检查这些选择器是否返回唯一(必需)元素。
WebElement element1 = driver.findElement(By.className("hidden-md-up"));
或者
WebElement element1 = driver.findElement(By.cssSelector(".link.hidden-md-up"));
推荐阅读
- spring-boot - 重定向到 html 文件时出现 Spring Boot 错误
- python-3.x - 将 gui py 文件转换为 exe 需要很长时间
- ruby - 管理 ruby gems 的系统依赖(例如 mysql2 和 openssl)
- azure-functions - 对 Graph 的 Azure Function 订阅
- python - 如何将 Pandas DataFrame 加载到 TensorFlow 中以进行高效的批量训练?
- r - 根据 tibble 中的数据发送电子邮件
- unix - 根据匹配字段删除第一个重复行并保留第二个匹配行
- xamarin.ios - IOS13如何发送静默推送通知?自更新后不起作用(Xamarin.ios)
- python-3.x - python从命令行训练和测试模型
- .htaccess - 使用 htpasswd 一次性登录