从列表项的未排序列表中查找选定的无线电输入标签 Selenium C# Xpath
页面该部分的屏幕截图:
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:
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;
}
===============================================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论