HtmlElement让我得到一个空值 [英] HtmlElement is getting me a null value

查看:84
本文介绍了HtmlElement让我得到一个空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码的其余部分,如果有帮助的话:

 HtmlElementCollection theElementCollection; 
HtmlDocument filters = webBrowser1.Document;

theElementCollection = webBrowser1.Document.GetElementsByTagName( input);

HtmlElementCollection pCollection = filters.GetElementById( filterSortBy)。GetElementsByTagName( option);
列表< HtmlElement> pList = new List< HtmlElement>();
foreach (HtmlElement pItem in pCollection){pList.Add(pItem); }
HtmlElement pElement =
(HtmlElement)pList.Where(pOption = > pOption.GetAttribute( value)。等于( price_low_high,StringComparison.InvariantCultureIgnoreCase))。SingleOrDefault();

if (pElement.GetAttribute( value)。Equals( price_low_high))
{
pElement.SetAttribute( Selected price_low_high);
pElement.InvokeMember( 点击);
}

pList.Clear();
pCollection = filters.GetElementById( filterAvailability)。GetElementsByTagName( option);
foreach (HtmlElement avItem in pCollection)
{
pList。加入(avItem);
}

pElement = pList.Where(pOption = > pOption.GetAttribute( value)。等于( 1\" ))的SingleOrDefault(); // 在此语句之后,pElement设置为null。

if (pElement.GetAttribute( )。等于( 1))
{
pElement.SetAttribute( Selected 1);
pElement.InvokeMember( 点击);
}



关于 pElement = pList.Where的评论中注明的错误点... 行。



是因为我希望价值等于1?



我是什么我想要做的是从下拉列表中选择名为1的值选项(在此网站中)



变量未在此声明中声明,因为它用于代码的其他方面。我尝试声明新的变量,但仍然是相同的结果



这是网站元素检查:



< pre lang =xml> < 选择 name = availability < span class =code-attribute> id = filterAvailability >
< 选项 value = label = 全部 已选择 = 已选择 > 所有< / option >
< 选项 value = 1 label = 有货(793) > 有货(793)< / option >
< 选项 value = 2 label = 预购(7) > 预购(7) < / option >
< / select >





我正在考虑更多这个问题,我认为问题是我制作的元素列表,因为它没有捕获数字值,只有文字。这是我的假设。是否有任何列表可以接受可用于webbrowser的数字?



谢谢。

解决方案

如果没有 pList .SingleOrDefault()将返回 null c $ c>传递 .Where()条件。

所以,你确定 pList 实际上包含 HtmlElement 选项,其属性为value== 1 ??


  if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
Application.DoEvents();
bookCollection = webBrowser1.Document.GetElementById( filterAvailability)。GetElementsByTagName( 选项);

列表< HtmlElement> avList = new 列表< HtmlElement>();
foreach (HtmlElement avItem in bookCollection)
{
avList。加入(avItem);
}

pElement = avList.Where(avOption = > avOption.GetAttribute( value)。等于( 1\" ))FirstOrDefault();

if (pElement.GetAttribute( value)。Equals( 1))
{
pElement.SetAttribute( Selected 1);
pElement.InvokeMember( 点击);
}


}







谢谢对你们两个我解决了:)谢谢你们所有的帮助


 nope!您必须使用该块等待 应用程序才能加载页面

if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// 此处页面已满载
< span class =code-comment> // 继续你的逻辑
bookCollection = webBrowser1.Document.GetElementById( filterAvailability)。GetElementsByTagName( 选项);

列表< HtmlElement> avList = new 列表< HtmlElement>();
foreach (HtmlElement avItem in bookCollection)
{
avList。加入(avItem);
}

pElement = avList.Where(avOption = > avOption.GetAttribute( value)。等于( 1\" ))FirstOrDefault();

if (pElement.GetAttribute( value)。Equals( 1))
{
pElement.SetAttribute( Selected 1);
pElement.InvokeMember( 点击);
}


Here is the rest of the code if it may help:

          HtmlElementCollection theElementCollection;
          HtmlDocument filters = webBrowser1.Document;

          theElementCollection = webBrowser1.Document.GetElementsByTagName("input");

          HtmlElementCollection pCollection = filters.GetElementById("filterSortBy").GetElementsByTagName("option");
          List<HtmlElement> pList = new List<HtmlElement>();
          foreach (HtmlElement pItem in pCollection) { pList.Add(pItem); }
          HtmlElement pElement =
            (HtmlElement)pList.Where(pOption => pOption.GetAttribute("value").Equals("price_low_high", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();

          if (pElement.GetAttribute("value").Equals("price_low_high"))
          {
              pElement.SetAttribute("Selected", "price_low_high");
              pElement.InvokeMember("click");
          }

          pList.Clear();
          pCollection = filters.GetElementById("filterAvailability").GetElementsByTagName("option");
          foreach (HtmlElement avItem in pCollection)
          {
              pList.Add(avItem);
          }

pElement = pList.Where(pOption => pOption.GetAttribute("value").Equals("1")).SingleOrDefault();  // After this statement the pElement is set to null.

          if (pElement.GetAttribute("value").Equals("1"))
          {
              pElement.SetAttribute("Selected", "1");
              pElement.InvokeMember("click");
          }


Error point noted in comment on pElement = pList.Where... line.

Is it because I want the value to be equal to 1?

What I am trying to do is select the value option named 1 (in this website) from a drop down list

The variables are not declared in this statement because it was used in other areas of the code. I tried declaring new variables but still the same result

This is the website element inspection:

<select name="availability" id="filterAvailability">
    <option value="" label="All" selected="selected">All</option>
    <option value="1" label="In stock (793)">In stock (793)</option>
    <option value="2" label="Pre-order (7)">Pre-order (7)</option>
</select>



I was thinking more about this problem and I think the problem is the Element list I made as it does not capture values that are numbers, only text. that is my assumption. Is there any list that accepts numbers that can be used for the webbrowser?

Thanks.

解决方案

The .SingleOrDefault() will return null if there are no elements of pList that pass the .Where() condition.
So, are you sure the pList actually contains the option HtmlElement with the attribute "value" == 1??


if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
       {
           Application.DoEvents();
           bookCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");

           List<HtmlElement> avList = new List<HtmlElement>();
           foreach (HtmlElement avItem in bookCollection)
           {
               avList.Add(avItem);
           }

           pElement = avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).FirstOrDefault();

           if (pElement.GetAttribute("value").Equals("1"))
           {
               pElement.SetAttribute("Selected", "1");
               pElement.InvokeMember("click");
           }


       }




thanks to both of you I solved it :) Thanks you for all the help


nope! you have to use that block to wait for the application to load the page

if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
       {
           Application.DoEvents(); 
       }
//here the page is fully loaded
//continue your logic
          bookCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");
 
           List<HtmlElement> avList = new List<HtmlElement>();
           foreach (HtmlElement avItem in bookCollection)
           {
               avList.Add(avItem);
           }
 
           pElement = avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).FirstOrDefault();
 
           if (pElement.GetAttribute("value").Equals("1"))
           {
               pElement.SetAttribute("Selected", "1");
               pElement.InvokeMember("click");
           }


这篇关于HtmlElement让我得到一个空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆