HtmlElement setAttribute访问被拒绝

时间:2014-11-11 14:17:46

标签: c# webbrowser-control async-await task

我有一个名为Input.cs的类,我在构造函数中传递现有的表单实例。

然后是这段代码:

private async Task PopulateInput(HtmlElement file, string value)
{
    file.Focus();
    var populateTask = Task.Delay(500).ContinueWith((_) =>
    {
        file.SetAttribute("value", value); // <- ACCESS DENIED ERROR
    }, TaskScheduler.FromCurrentSynchronizationContext());

    await populateTask;

    await Task.Delay(500);
}

public async Task Populate(string fieldSelector, string fieldIdentificator, string value, string fieldType = "input")
{
    var elements = mainForm.webBrowser.Document.GetElementsByTagName(fieldType);
    foreach (HtmlElement file in elements)
    {
        if (file.GetAttribute(fieldSelector) == fieldIdentificator) // Example file(input?) name == title
        {
            file.Focus();
            if (fieldType == "input")
            {
                await PopulateInput(file, value);
            }
            else if (fieldType == "upload")
            {
                await PopulateUploadFile(file, value);
            }
        }
    }
}

这一行:file.SetAttribute("value", value);会产生一个ACCESS DENIED错误,我不知道该代码有什么问题。

确切错误:

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.Forms.dll

(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

调用方法:

input.Populate(fieldType, fieldIdentificator, value).ContinueWith((_) =>
            {
                MessageBox.Show("Field populated!");
            }, TaskScheduler.FromCurrentSynchronizationContext());

0 个答案:

没有答案