Следующий код извлекает «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]);
}
Небольшой фон:
Изначально я думал, что возвращение значения должно быть таким же простымas:
// ATTENTION: THIS DOESN'T WORK
webBrowser1.InvokeScript("eval", "return 'hello';");
Но это всегда выдает SystemException
(«Произошла неизвестная ошибка. Ошибка: 80020101.»). Это причина, почему код выше сначала определяет функцию JavaScript, которая возвращает желаемыйзначение.Затем мы вызываем эту функцию, и результат возвращается успешно.