HTML
<div class="WIS wd-popup wdappchrome-ad" data-popup-version="2" data-automation-id="searchInputAutocompletePopup" data-automation-activepopup="true" style="padding: 7px 0px; right: auto; bottom: auto; z-index: 50; top: 92px; left: 65px;">
<div class="WFS wd-popup-content wdappchrome-ae" style="max-height: 195px; max-width: 1282px;">
<ul class="wdappchrome-ah" aria-setsize="3">
<li class="wdappchrome-ai" data-automation-id="searchInputAutoCompleteResult" tabindex="0" aria-posinset="1"><span data-automation-id="searchInputAutoCompleteResultFullText"><span style="font-weight:500"><span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Build</span> <span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Your</span>-<span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Resume</span> <span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">first</span> Plan</span>
</span><span class="wdappchrome-aj" data-automation-id="searchInputAutoCompleteResultType"> - Feature</span></li>
<li class="wdappchrome-ai" data-automation-id="searchInputAutoCompleteResult" tabindex="0" aria-posinset="2"><span data-automation-id="searchInputAutoCompleteResultFullText"><span style="font-weight:500"><span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Build</span> <span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Your</span>-<span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Resume</span> <span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Second</span> Plan Configurable Category</span>
</span><span class="wdappchrome-aj" data-automation-id="searchInputAutoCompleteResultType"> - Feature</span></li>
<li class="wdappchrome-ai" data-automation-id="searchInputAutoCompleteResult" tabindex="0" aria-posinset="3"><span data-automation-id="searchInputAutoCompleteResultFullText"><span style="font-weight:500"><span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Build</span> <span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Your</span>-<span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Resume</span> <span style="font-weight:400" data-automation-id="searchInputAutoCompleteResultToken">Third</span> Plan Schedule</span>
</span><span class="wdappchrome-aj" data-automation-id="searchInputAutoCompleteResultType"> - Feature</span></li>
</ul>
</div>
<div role="presentation" class="WMS wd-popup-anchor WPS WPR"></div>
</div>
我尝试从自动填充结果中选择选项
try {
WebElement autoOptions =driver.findElement(By.className("wdappchrome-ah"));
List<WebElement> optionsToSelect = autoOptions.findElements(By.tagName("li"));
System.out.println("TExt : " + optionsToSelect.get(0).getText());
System.out.println(optionsToSelect.get(1).getText());
System.out.println(optionsToSelect.get(2).getText());
for (WebElement option : optionsToSelect) {
if (option.getText().equals(textToSelect)) {
System.out.println("Trying to select: " + textToSelect);
option.click();
break;
}
}
} catch (NoSuchElementException e) {
System.out.println(e.getStackTrace());
} catch (Exception e) {
System.out.println(e.getStackTrace());
}}
自动化步骤
Build your own resume
问题我正面临
我使用上面的代码获得正确的li
号码,但是当我GETTEXT
时,它会将空白返回给我。结果来自li,然后使用span
分割整个搜索词,以及我无法管理并获得全文的内容。
答案 0 :(得分:1)
我不熟悉Java中的Selenium,但是你得到的结果似乎是合乎逻辑的,因为你试图从父元素而不是元素本身提取文本。
HTML中的更新:每个Li的跨度包含沿着跨度分布的文本3次,您只需要从第一个跨度获取文本。
尝试类似:
String textToSelect;
List<WebElement> optionsToSelect = autoOptions.findElements(By.tagName("li"));
for (WebElement option : optionsToSelect) {
textToSelect="";
List<WebElement> textPerLi= option .findElements(By.tagName("span"));
textToSelect=textToSelect+textPerLi.get(0).getText();
if ("Text you want to match with".equals(textToSelect)) {
System.out.println("Trying to select: " + textToSelect);
option.click();
break;
}
}
答案 1 :(得分:0)
要根据文本选择特定结果,您可以使用以下代码块:
List<WebElement> options = driver.findElements(By.xpath("//ul[@class='wdappchrome-ah']/li//span[@data-automation-id='searchAutoCompleteResultToken']"));
for (WebElement my_option:options)
{
String innerhtml = my_option.getAttribute("innerHTML");
if(innerhtml.contentEquals("textToSelect"))
{
my_option.click();
break;
}
System.out.println("Option chosen is : "+innerhtml);
}