Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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# 获取HttpElement文本_C#_Browser - Fatal编程技术网

C# 获取HttpElement文本

C# 获取HttpElement文本,c#,browser,C#,Browser,如何仅提取div1文本Div内部文本 div1.innerText返回和span的文本。关于获取元素的内部文本,也有类似的问题 解决方案1:见 解决方案2:使用HTML文档使用Javascript。方法 在HTML中: HtmlElement e1 = webBrowser1.Document.GetElementById("elementId"); string content = e1.InnerText MessageBox.Show(content); 关于获取元素的内部文本,也有类似

如何仅提取div1文本Div内部文本


div1.innerText返回和span的文本。

关于获取元素的内部文本,也有类似的问题

解决方案1:见

解决方案2:使用HTML文档使用Javascript。方法

在HTML中:

HtmlElement e1 = webBrowser1.Document.GetElementById("elementId");
string content = e1.InnerText
MessageBox.Show(content);

关于获取元素的内部文本,也有类似的问题

解决方案1:见

解决方案2:使用HTML文档使用Javascript。方法

在HTML中:

HtmlElement e1 = webBrowser1.Document.GetElementById("elementId");
string content = e1.InnerText
MessageBox.Show(content);

我将采用的方法是迭代子节点,测试每个节点是否为textnode,以及是否将其存储在数组中,然后返回连接的数组元素

Object[] objArray = new Object[1];
objArray[0] = (Object)"elementId";
string content = webBrowser1.Document.InvokeScript("getInnerText", objArray);
MessageBox.Show(content);
变量内容将包含内部文本值。
这不会检查传递给函数的id是否存在,因此实际应用程序需要进行额外的检查。

我将采取的方法是迭代子节点,测试每个节点是否为textnode,以及是否将其存储在数组中,然后返回连接的数组元素

Object[] objArray = new Object[1];
objArray[0] = (Object)"elementId";
string content = webBrowser1.Document.InvokeScript("getInnerText", objArray);
MessageBox.Show(content);
变量内容将包含内部文本值。
这不会检查传递给函数的id是否存在,因此实际应用程序需要额外检查。

C和webbrowser?你不是说Javascript吗?还有,你试过什么?它可能有助于理解..NET WebBrowser组件的工作方式和内容。已尝试HttpElement.InnerText它返回内部标记的文本。试图删除所有子元素,组件没有这样的属性或方法。C和webbrowser?你不是说Javascript吗?还有,你试过什么?它可能有助于理解..NET WebBrowser组件的工作方式和内容。已尝试HttpElement.InnerText它返回内部标记的文本。试图删除所有子元素,组件没有这样的属性或方法。这对于javascript很好,但是如何从中访问DOM。NET这对于javascript很好,但是如何从中访问DOM。NETSolution 1:这将去除所有html标记,并返回Span text1 Span tex2 Div内部文本。我只想要Div内部文本。解决方案2很好,但我无法更改源html。解决方案1:这将删除所有html标记,并返回Span text1 Span tex2 Div内部文本。我只想要Div内部文本。解决方案2很好,但我无法更改源html。
  function innerText(element){
    var i, text = [], child = null;
    for(i = 0; i < element.childNodes.length; i++){
      child = element.childNodes[i]

      if (child.nodeType === 3 &&
        child.nodeValue.match(/[^\n\s\t\r]/)){
        text.push(child.nodeValue);
      }
    }
    return text.join("");
  }
  // Example call
  alert(innerText(document.getElementById("div1")));
  function innerText(id){

    var i, text = [], child = null, element = document.getElementById(id);
    for(i = 0; i < element.childNodes.length; i++){
      child = element.childNodes[i]

      if (child.nodeType === 3 &&
        child.nodeValue.match(/[^\n\s\t\r]/)){
        text.push(child.nodeValue);
      }
    }
    return text.join("");
  }
string content = 
  (string)webBrowser1.Document.InvokeScript("innerText", 
                                            new string[] { "div1" });