简体   繁体   中英

Selenium - Select Item From List By The ul li Value Text

I've got the following HTML

<div id="colLeft_OrderGroups" class="GroupList GroupList_Left">
            <div class="SelectList" style="height:516px;">
                <div class="DialogSubtitle">Available groups</div>
                <ul class="ui-selectable" id="grdAvailableGroups" style="width:100%; margin-right:2px">
                <li value="10929">AppraisersGroupTest</li>
                </ul>
            </div>
        </div>

How do I select the option based off the "AppraisersGroupTest" text?

There will be multiple values in the list soon, so I need to be able to specify the text.

I have tried the answer in this post , but I'm getting syntax errors I cannot resolve.

You can achieve this using one liner xPath as below :-

String text = "AppraisersGroupTest";
WebElement el = driver.findElement(By.xpath("//div[@id = 'colLeft_OrderGroups']/descendant::li[text() = '" + text + "']"));
el.click();

Hope it will help you...:)

Looking at your HTML, I'm going to assume that the value of the desired LI is going to always be "10929" for your desired "AppraisersGroupTest." With that info, you can use the code below.

String value = "10929";
WebElement dropdown = driver.findElement(By.id("grdAvailableGroups"));
dropdown.click(); // assuming you have to click the "dropdown" to open it
dropdown.findElement(By.cssSelector("li[value=" + value + "]")).click();

If it turns out that is not a good assumption, you can use the code below to search for the desired text and click the element.

String searchText = "AppraisersGroupTest";
WebElement dropdown = driver.findElement(By.id("grdAvailableGroups"));
dropdown.click(); // assuming you have to click the "dropdown" to open it
List<WebElement> options = dropdown.findElements(By.tagName("li"));
for (WebElement option : options)
{
    if (option.getText().equals(searchText))
    {
        option.click(); // click the desired option
        break;
    }
}

'For anyone who may need it

Sub Memlogin()

'Login Members Profile
Dim IE As New selenium.WebDriver
Set IE = New ChromeDriver

With IE
        .Get "Your URL"

        .FindElementByName("Uni_user_id").SendKeys ("Your User ID")

        .FindElementByName("password").SendKeys ("Your Password")

        .FindElementById("Uni_login_button").Click

        'Select Profile Section
        .Get "Secondary URL if Needed"
 
        .Wait Now + TimeValue("00:00:10")
        
End With

'Select Desired Tab
Dim MTab, Tabs, User As selenium.WebElement
Set MTab = IE.FindElementByTag("ul")
Set Tabs = IE.FindElementByClass("li class-name ")
Set User = IE.FindElementByXPath("//a[text()=""li innerText""]")


IE.Actions.MoveToElement(User).Click(User).Perform

IE.Wait Now + TimeValue("00:00:20")

End Sub

  1. List item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM