【问题标题】:Null Value from GetElementById using C#使用 C# 来自 GetElementById 的空值
【发布时间】:2020-02-01 07:00:08
【问题描述】:

我已经解决了很多已经提出的问题,但我无法找到解决问题的方法。

我的应用程序是一个视频查找器,用户在文本框中输入他/她要查找的内容,然后从三个网站(Youtube、Metacafe、Screen.yahoo)中选择一个来查找视频。

我为每个选项都有一个方法,但是当它到达 GetElementByID 方法时,它会为所有三个选项返回一个空值。我会假设我错过了一些东西,这就是为什么我对所有 3 种方法都有这个空结果。

首先是Youtube方法

private void YouTube(String Input)
        {
            try
            {
                webBrowser1.Navigate("https://www.youtube.com/");
                HtmlDocument Doc = webBrowser1.Document;
                HtmlElement Search = Doc.GetElementById("search_query");
                Search.SetAttribute("value",Input);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }

        }

这是我尝试访问的搜索栏的元素(来自 youtube)。

<input id="masthead-search-term" autocomplete="off" autofocus="" onkeydown="if (!this.value &amp;&amp; (event.keyCode == 40 || event.keyCode == 32 || event.keyCode == 34)) {this.onkeydown = null; this.blur();}" class="search-term masthead-search-renderer-input yt-uix-form-input-bidi" name="search_query" value="" type="text" tabindex="1" placeholder="" title="Search" dir="ltr" spellcheck="false" style="outline: none;">

我已经尝试了这个元素的 id 和 name,但都给了我相同的结果。

不确定天气您是否需要其他两种方法,因为它们几乎相同,但我将发布它们以防万一。

这里是 metacafe 元素

private void Metacafe(String Input)
        {
            try
            {
                webBrowser1.Navigate("http://www.metacafe.com/");
                HtmlDocument Doc = webBrowser1.Document;
                HtmlElement Search = Doc.GetElementById("searchText");
                //webBrowser1.Document.GetElementById("searchText").SetAttribute("value", Input);
                Search.SetAttribute("value", Input);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }

        }

以及我试图连接的元素。

<input value="Search Metacafe" type="text" accesskey="s" class="TextField " title="Search Metacafe" autocomplete="off" size="50" name="searchText" tabindex="1" id="SearchQuery">

最后是 Yahoo 方法。

private void Yahoo(String Input)
        {
            try
            {
                webBrowser1.Navigate("https://screen.yahoo.com/");
                HtmlDocument Doc = webBrowser1.Document;
                HtmlElement Search = Doc.GetElementById("p");
                Search.SetAttribute("value", Input);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }

        }

及其元素。

<input id="UHSearchBox" type="text" class="yucs_W(100%) Fz(18px)! O(n):f Fw(200)! Bxz(bb) M(0)! Py(4px)! Bdrs(0)! Bxsh(n)" style="border-color: rgb(117, 144, 245); opacity: 1;" name="p" aria-describedby="UHSearchBox" data-ylk="slk:srchinpt-hddn;itc:1;" data-yltvsearch="https://video.search.yahoo.com/search/" data-yltvsearchsugg="/" data-satype="mini" data-gosurl="https://search.yahoo.com/sugg/ss/gossip-us_ss/" data-pubid="112" data-appid="" data-maxresults="10" data-resize=" " data-rapid_p="2">

感谢您花时间阅读。 /D

【问题讨论】:

  • 我认为如果您使用为 Youtube 和其他网站提供的 API,您的任务会更容易。可以查询站点并填写搜索表单,但如果页面在任何时候发生变化,您将不得不重写您的应用程序...在此处查看 youtube API:developers.google.com/youtube/v3,我确信其他站点有一些东西类似

标签: c# youtube null getelementbyid


【解决方案1】:

您正在尝试通过其Id 查找元素,但在GetElementById() 方法中,您提供了name 的元素,您需要通过它的Id 来查找元素,就像这样

HtmlElement Search = Doc.GetElementById("masthead-search-term");

对剩下的两个做同样的事情。

如果页面没有正确加载,这个元素也会是null 只有在页面有loaded completely之后才能访问这个元素。

编辑

您需要添加WebBrowserDocumentCompleted 事件。当WebBrowser 完成加载文档时发生此事件。

 private void YouTube(String Input)
    {
        try
        {
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
            webBrowser1.Navigate("https://www.youtube.com/");

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error");
        }

    }
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        HtmlDocument Doc = webBrowser1.Document;
        HtmlElement Search = Doc.GetElementById("search_query");
        Search.SetAttribute("value",Input);
    }

【讨论】:

  • 您好,再次感谢您的帮助。这解决了我的问题:] 只需要找到将输入变量发送到新方法即可。但再次感谢您的帮助。
  • 您可以创建一个全局变量并保存其值,然后在新方法中使用该变量。
【解决方案2】:

我在 youtube 上遇到了类似的问题。 GetElementById 未找到任何 ID。看起来问题是由其他浏览器引起的。 Opera 和 Edge 网站工具显示其他类名,然后“Document.Body.InnerHtml”包含。所以只需将“Document.Body.InnerHtml”复制到记事本中并找到我正在搜索的类名。

例如获取播放列表中所有歌曲的文件名。打开该播放列表并调用:

Document.GetElementsByTagName("li") 
go through all items and get video name with:
HtmlElement.GetAttribute("data-video-title") 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2014-02-10
    相关资源
    最近更新 更多