【发布时间】:2019-09-07 20:47:30
【问题描述】:
我的任务是点击下拉列表中的元素,完全使用 Select 类。
所以我有HTML 块:
<div id="_desktop_currency_selector">
<div class="currency-selector dropdown js-dropdown open">
<span>Валюта:</span>
<span class="expand-more _gray-darker hidden-sm-down" data-toggle="dropdown">UAH ₴</span>
<a data-target="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" class="hidden-sm-down">
<i class="material-icons expand-more"></i>
</a>
<ul class="dropdown-menu hidden-sm-down" aria-labelledby="dLabel" style="display: block;">
<li>
<a title="Евро" 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="Украинская гривна" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=1" class="dropdown-item">UAH ₴</a>
</li>
<li>
<a title="Доллар США" 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>
在后续尝试中,我有
org.openqa.selenium.TimeoutException:预期条件失败: 等待元素可点击:By.xpath: //*[@id='_desktop_currency_selector']//选择(尝试了30秒) 间隔为 500 毫秒)
click(currencyDropListBtn);
WebElement dropListBtn = driver.findElement(By.xpath("//*[@id='_desktop_currency_selector']//i"));
waitToBeClickable(dropListBtn);
dropListBtn.click();
WebElement dropListElement = driver.findElement(By.xpath("//*[@id='_desktop_currency_selector']//select"));
waitToBeClickable(dropListElement);
Select select = new Select(dropListElement);
select.selectByIndex(1);
它将按以下方式工作:
WebElement dropListBtn = driver.findElement(By.xpath("//*[@id='_desktop_currency_selector']//i"));
waitToBeClickable(dropListBtn);
dropListBtn.click();
WebElement dropListElement = driver.findElement(By.xpath("//a[@title='Евро']"));
waitToBeClickable(dropListElement);
click(dropListElement);
但我需要完全使用Select 类。
如何通过Select正确选择下拉列表元素?
【问题讨论】:
-
尝试使用
waitToBeClickable(dropListElement);而不是waitToBePresent(dropListElement);。 -
@RatmirAsanov 我确实升级了问题
-
你能分享页面的 URL 吗?