简体   繁体   中英

Two Questions about the WebBrowser Control

  1. How can I get an HtmlElement if the source code doesn't have the "id=''", but only has the "name=''". Document.GetElementById() doesn't work, and Document.All[""] doesn't work (obviously, because there is no ID.

  2. How can I simulate a form post?

Question 1 is more important because I found it harder to search. Question 2 I think i can get with more searching.

GetElementsByTagName could get you all of the tags that you are after, then you can look through the returned collection for the element you are after (if there is more then one).

This post shows how to submit the form element.

您也可以通过GetElementByName获取元素。

1. You can use the document.getElementsByName . It returns an array with all the elements that has the specified name (as names doesn't have to be unique like id:s).

2. You can use the submit method of a form element to post it. The form can either be an existing form in the page, or be created on the fly when you need it.

You can also use the XmlHttpRequest object to make a post, if you want to take care of the response yourself instead of having it load as a document.

There seems to be some confusion here, so I'll summarize:

On the document object, there are a number of methods you can use to locate an element:

  • document.getElementById('myid'); will return a single DOM element (if it exists) with an id attribute equal to myid .
  • document.getElementsByName('myname'); will return an array of DOM elements with a name attribute equal to myname .
  • document.getElementsByTagName('div'); will return an array of DOM elements with a matching tag - in this case, all div s in the document.

The case is important.

Does that clear things up?

This should work with all browsers provided you're not trying to get elements inside an iframe or something like that, but if you're having trouble with different browsers, I'd suggest using something like jQuery. It abstracts the browser differences from you so you can use the same syntax regardless of the browser being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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