Reputation: 1128
I have lot of li
tags under div
and ul
. Each li
tag consists of strong
tag:
<div style="width:380px" id="autoComplete" class="suggest fl">
<div class="sWrap">
<div class="iconWrap">
<span class="nLoder" style="display: none;"></span>
</div>
<div>
<input type="text" autocomplete="off" placeholder="Search" class="" id="keywords1" style="width:375px;" name="KEYWORDS">
</div>
</div>
<div class="sugCont nScroll " id="sugDrp_autoComplete" style="display: none; width: 380px;">
<ul class="Sdrop">
<li class="sugTouple">
<button style="width:100%" class="Sbtn " tabindex="-1" type="button">
Acc
<strong>ounting</strong>
</button>
</li>
<li class="sugTouple">
<button style="width:100%" class="Sbtn" tabindex="-1" type="button">
Acc
<strong>ounts Payable</strong>
</button>
</li>
<li class="sugTouple">
<button style="width:100%" class="Sbtn" tabindex="-1" type="button">
Acc
<strong>ounts Receivable</strong>
</button>
</li>
<li class="sugTouple">
<button style="width:100%" class="Sbtn" tabindex="-1" type="button">
Acc
<strong>ount Management</strong>
</button>
</li>
</ul>
</div>
</div>
My current code:
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//ul[@class='Sdrop']"));
for (WebElement option : optionsToSelect) {
System.out.println(option);
if (option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}
How to select the value? I am missing something, guide me to reach.
Upvotes: 0
Views: 88
Reputation: 1205
You should count li then start loop and check button text here is updated code.
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@id='sugDrp_autoComplete']/ul[@class='Sdrop']/li"));
for (WebElement option : optionsToSelect) {
WebElement buttonObj = option.findElement(By.tagName("button"))
if (buttonObj.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
buttonObj .click();
break;
}
}
Upvotes: 1
Reputation: 443
You can directly click the button using the xpath:
//button[@class='Sbtn' and text()='textToSelect']
Upvotes: 0