【问题标题】:Get attribute value from HTML element in WebBrowser从 WebBrowser 中的 HTML 元素获取属性值
【发布时间】:2011-11-24 20:03:09
【问题描述】:

我需要括号中的数字,在本例中为“14”,以便我可以将其发送到页面上的 JavaScript 函数以提交,或者有没有办法“单击”按钮“当前周”?

<form id="timetablesForm" action="timetablesController.jsp" method="post">
    <p>
        <input type="button" name="Current week" value="Current week" onclick="javascript:void+displayTimetable('14')" />
        <input type="button" name="Next week" value="Next week" onclick="javascript:void+displayTimetable('15')" />
        <input type="button" name="Current semester" value="Semester 1" onclick="javascript:void displayTimetable('7;8;9;10;11;12;13;14;15;16;17;21')" />
    </p>

function displayTimetable(selectdweeks)

我在 Windows Phone 中工作,所以WebBrowser.Document 不可用。我使用下面的脚本以不同的形式设置了一个值:

webBrowser1.InvokeScript("eval", "document.getElementById('loginform1').user.value = 'username';");

如何获取 HTML 页面中的值?我还能用InvokeScript 调用什么其他函数?如果您能指出功能列表,请不胜感激。

【问题讨论】:

    标签: c# javascript html windows-phone-7


    【解决方案1】:

    使用这个

    webBrowser1.SaveToString()
    

    然后解析 HTML 以获得我想要的位

    【讨论】:

      【解决方案2】:

      以下代码从您的第一个输入“当前周”中提取“14”:

      private void button1_Click(object sender, RoutedEventArgs e)
      {
          // first define a new function which returns the content of "onclick" as string
          webBrowser1.InvokeScript("eval", "this.newfunc_getmyvalue = function() { return document.getElementById('Current week').attributes.getNamedItem('onclick').value; }");
          // now invoke the function and save the result (which will be "javascript:void+displayTimetable('14')")
          string onclickstring = (string)webBrowser1.InvokeScript("newfunc_getmyvalue");
          // now split the string at the single quotes (you might have to adapt this parsing logic to whatever string you expect; this is just a simple approach)
          string[] substrings = Regex.Split(onclickstring, "'");
          // this shows a messagebox saying "14"
          MessageBox.Show(substrings[1]);
      }
      

      一点背景:

      最初我认为返回值应该像这样简单:

      // ATTENTION: THIS DOESN'T WORK
      webBrowser1.InvokeScript("eval", "return 'hello';");
      

      但这总是抛出SystemException(“发生未知错误。错误:80020101。”)这就是为什么上面的代码首先定义了一个返回所需值的JavaScript函数。然后我们调用这个函数,结果就成功返回了。

      【讨论】:

      • 第二个转换字符串行不断抛出相同的异常“错误:80020101”。是否可以使用此方法“单击”“当前周”按钮?我需要 14 或单击它。非常感谢
      • 奇怪。对我来说它有效。你在运行芒果吗?尝试webBrowser1.InvokeScript("eval", "document.getElementById('Current week').click()"); 进行点击。
      • 应用程序以 WP OS 7.1 为目标,该代码也为我引发了系统异常:(
      • 但是第一条InvokeScript 行有效吗?那么IsScriptEnabledTrue 对于WebBrowser 吗?您在某处有网页的实时示例吗?我刚刚有了你的 HTML sn-p 并使用 NavigateToString() 来加载它。也许这会有所作为。
      • 感谢您的所有努力,非常感谢,我想我只需要使用 SaveToString 并解析所有内容,因此只剩下数字,丑陋但简单:P跨度>
      【解决方案3】:

      您是否尝试过这样做并获得价值?

      Object val = webBrowser1.InvokeScript("eval", "document.getElementById('loginform1').user.value;");
      

      【讨论】:

      • 已经尝试了Object val = webBrowser1.InvokeScript("eval", "document.getElementById('timetablesForm').'Current week'.onclick.value;"); 的许多变体,但都抛出了 SystemException
      猜你喜欢
      • 2020-01-28
      • 2012-01-20
      • 2012-01-13
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多