C#WebBrowser运行Javascript-返回带有Javascript结果的空白页面-为什么?

C#WebBrowser运行Javascript-返回带有Javascript结果的空白页面-为什么?,c#,javascript,browser,C#,Javascript,Browser,当我尝试在webbrowser中运行javascript时,代码会运行,但在执行之后,它会将我带到一个白色页面上,页面右上角有一个数字值(在本例中为“1000”),将我带离以前所在的站点 HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement scriptEl = webBrowser1.Document.CreateElement("script"); IHTMLScriptEle

当我尝试在webbrowser中运行javascript时,代码会运行,但在执行之后,它会将我带到一个白色页面上,页面右上角有一个数字值(在本例中为“1000”),将我带离以前所在的站点

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function ScrollDown() { document.getElementsByClassName('scrollableitemclass').scrollTop = 1000 }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("ScrollDown");

感谢您的帮助

您可以滚动到您的Html浏览器

请参见此示例:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = "<html><body><span class=\"cls\" id=\"el\"> </body></html>";
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //for element with id
            webBrowser1.Document.GetElementById("el").ScrollIntoView(true);
            //for element with spesific
            foreach (HtmlElement el in webBrowser1.Document.All)
            {
                if (el.GetAttribute("ClassName") == "cls")
                {
                    el.ScrollIntoView(true);
                }

            }

        }


    }
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
webBrowser1.DocumentCompleted+=新的WebBrowserDocumentCompletedEventHandler(webBrowser1\u DocumentCompleted);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
webBrowser1.DocumentText=“”;
}
作废webBrowser1\u DocumentCompleted(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
//对于id为的元素
webBrowser1.Document.GetElementById(“el”).ScrollIntoView(true);
//对于具有特殊性的元素
foreach(webBrowser1.Document.All中的HtmlElement el)
{
if(el.GetAttribute(“类名称”)=“cls”)
{
el.ScrollingToView(真);
}
}
}
}

是否要滚动到web浏览器控件中的特定元素?是的,该功能可以工作,但在执行后,webbrowser会在右上角的白色页面上显示一个数值(在本例中为“1000”)