简体   繁体   English

使用从Selenium C#中的CheckBoxList中提取的标签填充CheckedListBox

[英]Populating a CheckedListBox with extracted labels from a CheckBoxList in Selenium C#

I have a form that uses Selenium to go to a page and automate tasks for a user. 我有一个使用Selenium进入页面并为用户自动执行任务的表单。 The only part of the page that changes is a CheckBoxList, and I've been trying to extract the labels from it and mirror them to my form's CheckedListBox so users can make the selection there without seeing the page. 页面上唯一更改的部分是CheckBoxList,并且我一直在尝试从中提取标签并将它们镜像到表单的CheckedListBox,以便用户可以在此处选择而无需查看页面。

So far I have this: 到目前为止,我有这个:

        IList<IWebElement> vehicleGroups = Builder.Driver.FindElements(By.ClassName("vehGrp"));
        String[] vehicleText = new String[vehicleGroups.Count];
        int i = 0;
        foreach (IWebElement element in vehicleGroups)
        {
            vehicleText[i++] = element.Text;
            vehicleGroupList.Items.Add(element.Text);
        }

Which works as far as getting the correct number of elements and populating the form, but all of the labels in vehicleText are blank (or just a space.) 只要获得正确数量的元素并填充表单,该方法就可以正常工作,但是vehicleText中的所有标签都是空白(或只是一个空格)。

An example of the HTML for one of the labels is 标签之一的HTML的示例是

    <label><input type="checkbox" name="searchQuery.vehicleGroups[0].isSelected" value="on" class="vehGrp">&nbsp;abcd/efgh ijkl mn (opqrst)</label>

Did I miss something or is the " " messing with the label text? 我错过了什么吗?还是“”弄乱了标签文本? The "abcd/efgh ijkl mn (opqrst)" is what I need but it and the potential number of elements can change daily. 我需要的是“ abcd / efgh ijkl mn(opqrst)”,但是它和元素的潜在数量可以每天更改。

vehicleGroups are the <input> elements, not the <label> s that surround them - and these have no text. vehicleGroups<input>元素,而不是包围它们的<label> ,并且它们没有文本。 This is expected behavior. 这是预期的行为。

You need to get the surrounding <label> element, for example using a method like this: https://stackoverflow.com/a/12194481/7866667 您需要使用以下方法获取周围的<label>元素: https : //stackoverflow.com/a/12194481/7866667

var vehicleGroupInputElements = driver.FindElements(By.ClassName("vehGrp"));
var vehicleGroupNames = vehicleGroupInputElements
    .Select(e => e.FindElement(By.XPath("..")))
    .Select(e => e.Text)
    .ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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