简体   繁体   English

从WebBrowser中的HTML元素获取属性值

[英]Get attribute value from HTML element in WebBrowser

I need the number in brackets, in this case "14" so that I can send it to a JavaScript function on the page to submit or alternatively is there a way to 'click' the button "Current week"? 我需要括号中的数字,在本例中为“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)

I'm working in Windows Phone so the WebBrowser.Document isn't available. 我在Windows Phone中工作,因此WebBrowser.Document不可用。 I've used the script below to set a value in a different form: 我使用下面的脚本以其他形式设置值:

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

How do I get a value from the HTML page? 如何从HTML页面获取值? What other function can I call with InvokeScript ? 我可以用InvokeScript调用哪些其他函数? Appreciate it if you can point me at a list of functions. 如果您能指出我的功能列表,请多谢。

Use this 用这个

webBrowser1.SaveToString()

Then parse the HTML to get the bit I want 然后解析HTML以获得我想要的位

The following code extracts the "14" from your first input "Current week": 以下代码从您的第一个输入“当前星期”中提取“ 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]);
}

A little background: 一点背景:

Initially I thought that returning a value should be as easy as: 最初我认为返回一个值应该像下面这样简单:

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

But this always throws a SystemException ("An unknown error has occurred. Error: 80020101.") This is the reason why the code above first defines a JavaScript function which returns the desired value. 但是这总是抛出一个SystemException (“发生了一个未知错误。错误:80020101。”)这就是为什么上面的代码首先定义一个返回所需值的JavaScript函数的原因。 Then we call this function and the result is returned successfully. 然后我们调用此函数,结果将成功返回。

你有没有试过这样做,获得价值?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM