WPF - Webbrowser - getElementById

3
在一个 WPF 应用程序中,我有一个名为 WebBrowser1 的 Web 浏览器。它指的是一个包含 TextArea 的 HTML 页面,用户可以在其中输入文本。
<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>

我希望获取这段文本,可能还要设置这段文本。
我尝试过类似于 JavaScript 的写法:
document.getElementById("myStudentOutput1").innerHTML;

例如
HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];

dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;

但它不起作用。

请在下面的HTML代码中添加您想要包含的内容: 包含相关内容

这里是您想要包含的内容。

如果您需要帮助,请联系我们的技术支持。

- lloyd
调试时它在哪一行失败了?有任何值是 null 吗? - lloyd
它只是说'WebBrowser'不包含'Document'和'GetElementsbyId'的定义,也找不到任何扩展方法(我是C#的新手)。 - LBogaardt
请查看 Visual Studios调试 - lloyd
为什么会有踩票?这是一个有效的问题,并提供了一个可工作的问题示例,下面的答案表明在解决问题之前需要采取一些非常规的步骤。 - LBogaardt
正如Hans Passant所指出的那样,“它不起作用”没有任何有用的意义。请查看你尝试过什么如何提问。如果您想要更深入的讨论,请在meta SO上创建一个问题。 - lloyd
2个回答

6
以下是适用于我在Visual Studio 2015 WPF应用程序中的解决方案。
首先,在项目中进行“添加引用”时,通过COM选项卡添加对Microsoft HTML COM库的引用。
然后,添加以下代码:
<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Left" Height="480" Margin="10,10,0,0" VerticalAlignment="Top" Width="770" Source="E:\Others\Dropbox\Programming\Questions.html"/>
        <Button x:Name="mySetQuestionButton" Content="Set Question" HorizontalAlignment="Left" Margin="200,520,0,0" VerticalAlignment="Top" Width="75" Click="mySetQuestion"/>
        <Button x:Name="myGetAnswerButton" Content="Get Answer" HorizontalAlignment="Left" Margin="350,520,0,0" VerticalAlignment="Top" Width="75" Click="myGetAnswer"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="600,520,0,0" TextWrapping="Wrap" Text="Hello2" VerticalAlignment="Top"/>
    </Grid>
</Window>

并且

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void mySetQuestion(object sender, EventArgs e)
        {
            mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
            mshtml.IHTMLElement textArea = document.getElementById("myQuestion1");
            textArea.innerHTML = "What is 1+1?";
        }
        private void myGetAnswer(object sender, EventArgs e)
        {
            mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
            mshtml.IHTMLElement textArea = document.getElementById("myStudentInput1");
            textBlock.Text = textArea.innerHTML;
        }
    }
}

2

但它没有起作用。

我不知道这可能意味着什么。您能得到的只是一个有效的代码片段:

public partial class Form1 : Form {
    private WebBrowser webBrowser1;
    private Button button1;

    public Form1() {
        button1 = new Button { Text = "Test" };
        button1.Click += button1_Click;
        this.Controls.Add(button1);
        webBrowser1 = new WebBrowser { Dock = DockStyle.Fill };
        webBrowser1.DocumentText = @"<html><body><textarea class=""myStudentInput"" id=""myStudentInput1"">Text to be copied</textarea></body></html>";
        this.Controls.Add(webBrowser1);
    }

    private void button1_Click(object sender, EventArgs e) {
        var elem = webBrowser1.Document.GetElementById("myStudentInput1");
        MessageBox.Show(elem.InnerText);
    }
}

这将产生:

输入图像描述


谢谢你的努力。 - LBogaardt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接