Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c收集表-webdriver中的所有div id_C#_Html_Selenium - Fatal编程技术网

C# 使用c收集表-webdriver中的所有div id

C# 使用c收集表-webdriver中的所有div id,c#,html,selenium,C#,Html,Selenium,预期: 我想收集所有div id并将其存储在一个数组中 我在用c。我怎么做?请帮帮我,伙计们第一步是创建一个XmlDocument。您需要包括System.Xml命名空间,如下所示: <table id="tblRenewalAgent" cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td> <div class="form-row"> <div id="trS

预期:

我想收集所有div id并将其存储在一个数组中
我在用c。我怎么做?请帮帮我,伙计们第一步是创建一个XmlDocument。您需要包括System.Xml命名空间,如下所示:

<table id="tblRenewalAgent" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<div class="form-row">
<div id="trStatus" style="">
<div id="trFees" class="form-row" style="">
<div id="trFees1" class="form-row ctrl-column" style="">
<div id="trFilingReceipt" class="form-row" style="">
<div id="trComments" class="form-row" style="">
<div id="trContact" class="form-row" style="">
<div id="trEmail" class="form-row" style="">
<div id="trPhone" class="form-row" style="">
<div id="trCell" class="form-row" style="">
<div class="form-row">
<div class="form-row ctrl-column">
<div id="trAmountPaid">
<div id="trBalanceDue" class="form-row">
</td>
</tr>
</tbody>
</table>
WebDriverWait wait = new WebDriverWait(driver, timeout);
            List<WebElement> totalrow  =  wait.until(ExpectedConditions
                    .presenceOfAllElementsLocatedBy(By.Id("tblRenewalAgent"));

        for (int i = 0; i <= totalrow.size(); i++) {

            String clasRow = //form-row;

            try {

                WebElement element = ExpectedBehaviors
                        .expectPresenceofElementBy(driver, By.class(clasRow),
                                getAlertTimeout());

                if (element.getText().trim().equals("EXPECTED VALUE")) {

                    //"PERFORM YOUR ACTION"                     
                    break;
                }

            } catch (Exception e) {
                //"You can add your exception and error message

            }
        }
下面我创建了一个示例应用程序,我将对此进行解释:

首先,您应该创建xml文档对象并加载一些xml。为了演示,我已经逐字地包含了您的HTML,但是您可以通过加载一个文件来替换它

using System.Xml;
WalkForIds方法递归地查看HTML以查找所有ID。定义如下:

foreach (XmlNode elem in html.ChildNodes)
    WalkForIds(elem);

在此之后,您会发现ids通用列表将包含HTML中的所有ID。

您的问题目前还不清楚。这个HTML是C程序中的字符串吗?如果是这样的话,我会考虑两个选项:1正则表达式来查找div或2 HTMLaGracyPACK中的每个ID,以清除畸形的HTML,然后使用XPath将所有div语句选择为一个集合,然后将允许您从那里选择所有ID。
static List<string> ids = new List<string>();
foreach (XmlNode elem in html.ChildNodes)
    WalkForIds(elem);
    private static void WalkForIds(XmlNode node)
    {
        if(node.HasChildNodes)
            foreach (XmlNode child in node.ChildNodes)
                WalkForIds(child);
        if (node.Attributes == null) return;
        foreach (XmlAttribute attr in node.Attributes)
            if (attr.Name == "id")
                ids.Add(attr.Value);

    }