c# - WPF-Webbrowser-getElementById

标签 c# webbrowser-control getelementbyid

在 WPF 应用程序中,我有一个名为 WebBrowser1 的网络浏览器。这是指包含用户可以输入文本的 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;

但它不起作用。

最佳答案

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

关于c# - WPF-Webbrowser-getElementById,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30664932/

相关文章:

C# 将通用对象转换为 bool 值

c# - 如何在 wpf 应用程序中使用 mshtml

c# - WinForms Web 浏览器控件强制重新聚焦?

javascript - 如何在元素html内添加元素数据属性值

javascript - 如何将对象转换为字符串

c# - 将一个类的对象转换为另一个类的对象

c# - CIL - 装箱/拆箱与可空

c# - 在多对多关系中定义 FK 名称

.net - 浏览器内存问题

javascript - 如何使用php在文本框中输入值并通过javascript获取它