Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual C# Express Edition
>
GetAttribute gives null?
GetAttribute gives null?
- I recently have been trying to figure out why i get a null value when i use this code:
I will get a null value. If i go into IE and check out the innerhtml i can see the src="image.gif" in there. Why would this happen? Thanks!IHTMLDocument2 currentdoc = this.wb.Document.DomDocument as IHTMLDocument2; string imagelocation = currentdoc.activeElement.getAttribute("src").ToString();
OMG, its Joe Ginley!
Answers
- Hi,
I checked and this is working fine for me,
since I browse google mail page in english.(not localized version). In localized versions the logo is in div's background and set by style.
public Form1() { InitializeComponent(); webBrowser1.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); } private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("http://www.google.com"); } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Document.MouseMove += new HtmlElementEventHandler(Document_MouseMove); } void Document_MouseMove(object sender, HtmlElementEventArgs e) { HtmlElement he = webBrowser1.Document.GetElementFromPoint(e.MousePosition); if (he != null) { if (he.GetAttribute("src") != "") { MessageBox.Show( he.GetAttribute("src")); } } }- Marked As Answer byJoeGinley Monday, November 16, 2009 10:40 PM
- Ok thanks will try to use .ToLower().EndsWith...
EDIT: Just got this working. I realize that putting a break in the else statement causes the foreach to stop and not continue till it hits the specific imagetype, while when it does get the imagetype (.gif) it will break...thanks!
new code:
foreach (string extension in Properties.Settings.Default.ImageTypes) { if (imagelocation.ToLower().EndsWith(extension)) { openImageLinkInNewTabTool.Enabled = true; imgopenInNewTabTool.Enabled = true; setAsBackgroundTool.Enabled = true; saveAsTool2.Enabled = true; break; } else { openImageLinkInNewTabTool.Enabled = false; imgopenInNewTabTool.Enabled = true; setAsBackgroundTool.Enabled = false; saveAsTool2.Enabled = false; } }
OMG, its Joe Ginley!- Marked As Answer byJoeGinley Monday, November 16, 2009 10:40 PM
All Replies
- Hi,
I think you are trying to extract all images from html.
You should use DocumentCompleted event to ensure all the html is loaded.
Here is a sample.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { foreach(HtmlElement he in webBrowser1.Document.GetElementsByTagName("img"))//get all the elements with tag name img { string location = he.GetAttribute("src");//then get their src attributes. } } - Isnt there a way i can just get it on a mousedown event or just get the active element when clicked? I tried your code and sometimes it returns the value of a completely different image than what i am currently selecting.
OMG, its Joe Ginley! - Hi,
Can you try this code.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Document.MouseMove += new HtmlElementEventHandler(Document_MouseMove); } void Document_MouseMove(object sender, HtmlElementEventArgs e) { HtmlElement he=webBrowser1.Document.GetElementFromPoint(e.MousePosition); if (he != null) { if (he.GetAttribute("src") != "") { this.Text = he.GetAttribute("src"); } } } - Thanks for the hint of code will try to use parts of it, since it does help. Ill let you know then.
OMG, its Joe Ginley! - Still not working, i even tried using the statustext, but since you cannot get the innerhtml from statustext it dont help much, so far the only way i really could get the image is if i go to the exact location, and i cannot retrieve it from a right click when a user sees it such as the google image displayed on their homepage.
OMG, its Joe Ginley! - Any thoughts on this...i still didnt figure it out really. Seems like i used to know though, but i lost my old code :(
OMG, its Joe Ginley! - HI,
What's happening when you move cursor over the image,
Whats the text of the form is showing.
Does mousemove event fire?
Does it get htmlelementcorrectly or gets null value. - The event does fire because i have it get the TagName to display at the bottom right...i get IMG...what exactly do you mean the text...the alternate text for an image? I get a null value for the html element attribute for src ONLY, but i can retrieve some other things? For example the google image on their main site, and i know you can get the src from that...
OMG, its Joe Ginley! - Hi,
I checked and this is working fine for me,
since I browse google mail page in english.(not localized version). In localized versions the logo is in div's background and set by style.
public Form1() { InitializeComponent(); webBrowser1.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); } private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("http://www.google.com"); } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Document.MouseMove += new HtmlElementEventHandler(Document_MouseMove); } void Document_MouseMove(object sender, HtmlElementEventArgs e) { HtmlElement he = webBrowser1.Document.GetElementFromPoint(e.MousePosition); if (he != null) { if (he.GetAttribute("src") != "") { MessageBox.Show( he.GetAttribute("src")); } } }- Marked As Answer byJoeGinley Monday, November 16, 2009 10:40 PM
- Ok it works like that and under a mousedown, i realize that its not working because of this for each statement, at least i think. Take a look at my code and please see what im doing wrong:
The ImagesTypes contains each image extension (.gif, .jpeg, .bmp, etc.), and imagelocation is the src string...so if the imagelocation ends with any of those image extensions why wouldnt it work.foreach (string extension in Properties.Settings.Default.ImageTypes) { if (imagelocation.EndsWith(extension)) { openImageLinkInNewTabTool.Enabled = true; imgopenInNewTabTool.Enabled = true; setAsBackgroundTool.Enabled = true; saveAsTool2.Enabled = true; break; } else { openImageLinkInNewTabTool.Enabled = false; imgopenInNewTabTool.Enabled = true; setAsBackgroundTool.Enabled = false; saveAsTool2.Enabled = false; break; } }
OMG, its Joe Ginley! - Hi,
What's the value of imagelocation,
EndWith is casesensitive so try. imageLocation.ToLower().EndsWith - Ok thanks will try to use .ToLower().EndsWith...
EDIT: Just got this working. I realize that putting a break in the else statement causes the foreach to stop and not continue till it hits the specific imagetype, while when it does get the imagetype (.gif) it will break...thanks!
new code:
foreach (string extension in Properties.Settings.Default.ImageTypes) { if (imagelocation.ToLower().EndsWith(extension)) { openImageLinkInNewTabTool.Enabled = true; imgopenInNewTabTool.Enabled = true; setAsBackgroundTool.Enabled = true; saveAsTool2.Enabled = true; break; } else { openImageLinkInNewTabTool.Enabled = false; imgopenInNewTabTool.Enabled = true; setAsBackgroundTool.Enabled = false; saveAsTool2.Enabled = false; } }
OMG, its Joe Ginley!- Marked As Answer byJoeGinley Monday, November 16, 2009 10:40 PM

