ASP.NET 和 C#:创建将关键字发送到带有搜索栏的网站的按钮

ASP.NET and C#: create button that sends keyword to website with search bar

我是 ASP.NET 和 C# 的新手。在网络应用程序中,我知道我可以创建一个打开网页的按钮:

private void button1_Click(object sender, EventArgs e)
{
    //Launch browser
    System.Diagnostics.Process.Start("https://www.nhl.com/jets");
}

但是如果着陆页有搜索栏,我如何才能在点击按钮后将关键字发送到该搜索栏?为了清楚起见,假设我的代码隐藏是这样声明该关键字的: string keyword = Keyword.Text。如何确保此关键字自动发送到搜索栏,以便用户无需键入关键字即可看到结果?

试试这个,你应该使用 webBrowser 自动化。我将我之前写的这个方法应用到你的网站上,只需添加你的小调整:

... string keyboard = Keyword.Text

    public String GetKeyboardValueForSearch() {
        return keyboard;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.DocumentCompleted += new 
        WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        webBrowser1.Navigate("https://www.nhl.com/jets");            
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        HtmlElement search = webBrowser1.Document.GetElementById("top-nav__search- 
        autocomplete__input");
        if(search != null)
        {
            search.SetAttribute("value", GetKeyboardValueForSearch());
            foreach(HtmlElement ele in search.Parent.Children)
            {
                if (ele.TagName.ToLower() == "input" && ele.Name.ToLower() == "go")
                {
                    ele.InvokeMember("click");
                    break;
                }
            }
        }
    }