使用C#从GetElementById中获取空值 [英] Null Value from GetElementById using C#

查看:316
本文介绍了使用C#从GetElementById中获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了很多已经提出的问题,但是我无法为我的问题找到解决方案.

I've gone through loads of already asked questions but I haven't been able to find a solution for my problem.

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

My application is a video finder, the user enters what he/she is looking for in a textbox and then selects from one of three websites(Youtube,Metacafe,Screen.yahoo) to find the video.

我为每个选择都有一个方法,但是当它到达GetElementByID方法时,它对所有三个选择都返回一个空值.我要假设我错过了一些东西,这就是为什么我在所有3种方法中都得到这个空结果的原因.

Ive got a method for each of the choices but when it reaches the GetElementByID method it returns a null value for all three. Im going to assume ive missed something and thats why im having this null result for all 3 methods.

首先是Youtube方法

Firstly here is the Youtube method

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)的元素.

Here is the elements for the search bar(from youtube) I'm trying to access.

<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,但是都给了我相同的结果.

I've tried both the id and name from this element but both have given me the same result.

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

Not sure weather you would need the other two methods seeing as they are almost identical but i'm going to post them just incase.

这是metacafe元素

Here is the metacafe element

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方法.

Lastly the Yahoo method.

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

Thanks for taking the time to read it. /D

推荐答案

您正在尝试通过其Id查找元素,但是在方法GetElementById()中,您给出的name元素需要通过其查找元素Id像这样

You are trying to find element by its Id but in the method GetElementById() you are giving name of element you need to find element by it's Id like this

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

对其余两个执行相同操作.

Do same for the rest of two.

如果页面未正确加载,则此元素将为null 页面只有loaded completely后才能访问此元素.

Also this element will be null if page hasn't loaded properly you can only access this element after page has loaded completely.

修改

您需要添加WebBrowserDocumentCompleted事件. WebBrowser完成文档加载后,将发生此事件.

You need to add DocumentCompleted event of WebBrowser. This event occurs when the WebBrowser has finished loading the document.

 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);
    }

这篇关于使用C#从GetElementById中获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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