Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在winforms中设置网站上生日下拉框中的值_C#_Winforms - Fatal编程技术网

C# 如何在winforms中设置网站上生日下拉框中的值

C# 如何在winforms中设置网站上生日下拉框中的值,c#,winforms,C#,Winforms,我想使用WinForms提供的内置web浏览器模块从网站上的下拉框中选择特定值或随机值 我尝试使用下面的代码,但它只是在框中留下了一个空白,如果提交,那么网站将返回一个错误,说出生日期没有指定 HtmlElement b_day = webBrowser1.Document.All["birthday_day"]; if (b_day != null) { var day = rnd.Next(1, 31).ToString(); b_day. = day; } 尝试使用从索引生成随

我想使用WinForms提供的内置web浏览器模块从网站上的下拉框中选择特定值或随机值

我尝试使用下面的代码,但它只是在框中留下了一个空白,如果提交,那么网站将返回一个错误,说出生日期没有指定

HtmlElement b_day = webBrowser1.Document.All["birthday_day"];
if (b_day != null) {
  var day = rnd.Next(1, 31).ToString();
  b_day. = day;
}

尝试使用从索引生成随机数。通过这种方式,它可以从该索引中随机选择您必须使用的成员。

您不能将
HtmlElement
对象设置为
字符串,您需要设置子
元素的正确属性。像这样的方法应该会奏效:

    private void btnChangeSelection_Click(object sender, EventArgs e)
    {
        Random rnd = new Random();
        HtmlDocument doc = webBrowser1.Document;
        HtmlElement daySelect = doc.GetElementById("birthday_day");

        if(daySelect != null)
        {
            int newSelection = rnd.Next(1, 31);
            foreach (HtmlElement option in daySelect.Children)
            {
                if(option.InnerText == newSelection.ToString())
                {
                    option.SetAttribute("selected", "true");
                    break;
                }
            }
        }
    }

在这里,我们循环查看
元素下可用的
并查找与我们的新随机值匹配的值。然后我们可以将该选项的
“selected”
属性设置为
“true”

对不起,我忘了指定,但我的问题是如何在Winforms中从下拉框中选择值?我尝试使用此选项,但它只是留下了空白,如果提交,网站将返回一个错误,说明未指定出生日期
HtmlElement b_day=webBrowser1.Document.All[“生日”];如果(b_day!=null){var day=rnd.Next(1,31).ToString();b_day.=day;}