WPF - Webbrowser - getElementById

时间:2015-06-05 11:03:20

标签: c# webbrowser-control getelementbyid

在WPF应用程序中,我有一个名为WebBrowser1的Web浏览器。这指的是一个HTML页面,其中包含用户可以输入文本的TextArea。

<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;

但它没有用。

2 个答案:

答案 0 :(得分:4)

Visual Studio 2015 WPF应用程序中的以下解决方案适用于我。

首先,添加对Microsoft HTML COM库的引用。当您在项目中执行“添加引用”时,这将在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;
        }
    }
}

答案 1 :(得分: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);
    }
}

产生:

enter image description here