C# 从WebBrowser中的HTML元素获取属性值
我需要括号中的数字,在本例中为“14”,以便我可以将其发送到页面上的JavaScript函数以提交,或者是否有方法“单击”按钮“当前周”C# 从WebBrowser中的HTML元素获取属性值,c#,javascript,html,windows-phone-7,C#,Javascript,Html,Windows Phone 7,我需要括号中的数字,在本例中为“14”,以便我可以将其发送到页面上的JavaScript函数以提交,或者是否有方法“单击”按钮“当前周” 如何从HTML页面获取值?使用InvokeScript还可以调用什么函数?如果您能给我一个函数列表,我将不胜感激。您是否尝试过这样做,以获取值 Object val = webBrowser1.InvokeScript("eval", "document.getElementById('loginform1').user.value;"); 以下代码从您的第
如何从HTML页面获取值?使用
InvokeScript
还可以调用什么函数?如果您能给我一个函数列表,我将不胜感激。您是否尝试过这样做,以获取值
Object val = webBrowser1.InvokeScript("eval", "document.getElementById('loginform1').user.value;");
以下代码从您的第一个输入“当前周”中提取“14”: 一点背景: 最初,我认为返回值应该很容易:
// ATTENTION: THIS DOESN'T WORK
webBrowser1.InvokeScript("eval", "return 'hello';");
但这总是抛出一个SystemException
(“发生了未知错误。错误:80020101”)。这就是上面的代码首先定义一个返回所需值的JavaScript函数的原因。然后调用此函数,结果将成功返回。使用此函数
webBrowser1.SaveToString()
然后解析HTML以获得我想要的位已经尝试了许多不同的
Object val=webBrowser1.InvokeScript(“eval”,“document.getElementById('timetablesForm')。'Current week'.onclick.value;')代码>但所有抛出系统异常第二个抛出的字符串行继续抛出相同的异常“Error:80020101”。是否可以使用此方法“单击”按钮“Current week”?我要么需要14,要么点击它。谢谢你。对我来说这很有效。你在经营芒果吗?尝试webBrowser1.InvokeScript(“eval”、“document.getElementById('Current week')。单击()
for click.app以WP OS 7.1为目标,该代码也为我抛出了一个系统异常:(但是第一行InvokeScript
起作用了吗?那么IsScriptEnabled
对于WebBrowser是否True
呢?你在什么地方有该网页的实时示例吗?我刚刚有你的HTML片段,并使用了NavigateToString()加载它。也许这会有所不同。谢谢你的所有努力,真的很感激,我想我必须使用SaveToString,解析所有内容,只留下数字,丑陋但简单:P
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';");
webBrowser1.SaveToString()