从列表项的未排序列表中查找选定的无线电输入标签 Selenium C# Xpath

发布于 2025-01-15 05:55:47 字数 2652 浏览 0 评论 0原文

页面该部分的屏幕截图:

read the value of selected item

HTML:

<ul id="c2231_Fields_147_Value" name="Fields_147_Value" cvlid="Cushion">          
    <li>        
        <input type="radio" name="Fields_147_Value" value="" id="c2231_Fields_147_Value-0">        
        <label for="c2231_Fields_147_Value-0">(not set)</label>      
    </li>          
    <li>        
        <input type="radio" name="Fields_147_Value" value="Low" id="c2231_Fields_147_Value-1">        
        <label for="c2231_Fields_147_Value-1">Low</label>      
    </li>          
    <li>        
        <input type="radio" name="Fields_147_Value" value="Max" id="c2231_Fields_147_Value-2">        
        <label for="c2231_Fields_147_Value-2">Max</label>      
    </li>          
    <li>        
        <input type="radio" name="Fields_147_Value" value="Moderate" id="c2231_Fields_147_Value-3">        
        <label for="c2231_Fields_147_Value-3">Moderate</label>      
    </li>      
</ul>

在上述元素中,一个单选是选定的, 我想要与所选无线电输入相对应的标签文本。

例如,如果选择了 'Max' 的单选,那么我想读取值 'Max'

在未排序列表下有 4 个输入单选和 4 个相应的标签。 每个列表项都是一组单选类型的输入和一个标签。

由于它是无线电类型,因此用户只能选择一种输入。

如何使用 C# 和 xpath 读取所选输入的值?

我编写了以下代码和xapths,但只有第一个xpath返回值,并且它不支持Iwebelement列表的列表,我想,因此我的IsSelected逻辑始终为真。

但是当我尝试使用 xpath 分别收集输入和收集标签时,两者都在检查时工作, 但是在 Visual Studio 中运行代码时,两者都返回空列表。

===========我的代码==========和xpaths

public IList<IWebElement> CushionItemsList { get { return driver.FindElements(By.XPath("//ul[@cvlid='Cushion']/li")); } }
public IList<IWebElement> CushionItemsListInputs { get { return driver.FindElements(By.XPath("//ul[@cvlid='Cushion']/li/input")); } }
public IList<IWebElement> CushionItemsListLabels { get { return driver.FindElements(By.XPath("//ul[@cvlid='Cushion']/li/label")); } }

public string GetSelectedCushionValue()
{
    string cushionValue = string.Empty;
    int x = CushionItemsListInputs.Count();

    for (int i = 0; i < CushionItemsListInputs.Count(); i++)
    {
        bool selected = CushionItemsListInputs[i].Selected;
        if (selected == true)
        {
            cushionValue = CushionItemsListLabels[i].Text;
        }
    }

    return cushionValue;
}

========================== ===============================

读取所选项目的值

A screenshot of that part of the page:

read the value of selected item

The HTML:

<ul id="c2231_Fields_147_Value" name="Fields_147_Value" cvlid="Cushion">          
    <li>        
        <input type="radio" name="Fields_147_Value" value="" id="c2231_Fields_147_Value-0">        
        <label for="c2231_Fields_147_Value-0">(not set)</label>      
    </li>          
    <li>        
        <input type="radio" name="Fields_147_Value" value="Low" id="c2231_Fields_147_Value-1">        
        <label for="c2231_Fields_147_Value-1">Low</label>      
    </li>          
    <li>        
        <input type="radio" name="Fields_147_Value" value="Max" id="c2231_Fields_147_Value-2">        
        <label for="c2231_Fields_147_Value-2">Max</label>      
    </li>          
    <li>        
        <input type="radio" name="Fields_147_Value" value="Moderate" id="c2231_Fields_147_Value-3">        
        <label for="c2231_Fields_147_Value-3">Moderate</label>      
    </li>      
</ul>

in the above elements, one radio is selected,
And I want the label text corresponding to the selected radio input.

For example, if the radio of 'Max' , is selected , then I want to read the value 'Max'

There are 4 input radio and 4 corresponding labels under unsorted list.
Each list item is a set of one input of type radio and one label.

User can select only one input since it is radio type.

How to read the value of selected input using C# and xpath ?

I have written following code and xapths but only the 1st xpath returned values , and it does not support list of list of Iwebelement, I guess, therefor my logic of IsSelected is always coming as true.

But when i tried with xpath for collection of input and collection of label separately, both worked at inspection time,
but while running the code in visual studio ,both returned empty list.

============my code=========and xpaths=========

public IList<IWebElement> CushionItemsList { get { return driver.FindElements(By.XPath("//ul[@cvlid='Cushion']/li")); } }
public IList<IWebElement> CushionItemsListInputs { get { return driver.FindElements(By.XPath("//ul[@cvlid='Cushion']/li/input")); } }
public IList<IWebElement> CushionItemsListLabels { get { return driver.FindElements(By.XPath("//ul[@cvlid='Cushion']/li/label")); } }

public string GetSelectedCushionValue()
{
    string cushionValue = string.Empty;
    int x = CushionItemsListInputs.Count();

    for (int i = 0; i < CushionItemsListInputs.Count(); i++)
    {
        bool selected = CushionItemsListInputs[i].Selected;
        if (selected == true)
        {
            cushionValue = CushionItemsListLabels[i].Text;
        }
    }

    return cushionValue;
}

===============================================

read value of selected item

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文