Here's what I am trying to do:
Select text from a webpage I pulled up using my web browser control.After clicking a button while this text is still selected I would like a message box to pop-up displaying the text that was highlighted by the user. How do I get this functionality to work in my wpf application?
I think I'm on the right track using mshtml but I get an error that says:
Error HRESULT E_FAIL has been returned from a call to a COM component.
This error will happen even when I try something small on the document like changing the title.
The code is below:
IHTMLDocument2 doc = (IHTMLDocument2)this.webBookText.Document;
doc.title = "l";
Well, for starters it would be a lot simpler to use WebBrowser than mshtml (note that you can still host WebBrowser in WPF) - this will certainly let you do simple things a lot easier:
webBook.Document.Title = "foo";
However, I can't see anything there that would let you work with selections very easily...
You can get the selected element with .Document.ActiveElement, but this is the entire element - not the selected portion.
Figured it out that error was because this wasn't in my form class
Related
I have an application that uses WebBrowser control to navigate from page to page, on some pages I get a leaving popup asking me if that's what I want to do.. this stops the whole further execution until I press "Leave" or "Stay".. How can I disable them?
What I've tried so far were these actions:
a) setting window.onbeforeunload = null;
b) setting alert, confirm, prompt to an empty function
c) settin suppressErrorMessages to true
but even so, I still get the nasty message in the end.
I mostly relied on this answer:
How to update DOM content inside WebBrowser Control in C#?
But so far without a success.
The alerts seem to be jQuery alerts because they have custom texts (instead of OK Cancel, they have Stay Leave)..
Any help hugely appreciated!!
The webbrowser control uses IE internally and IE has a prompt if you've filled out a form asking you if you really want to leave the page (thereby losing the content you've filled out) perhaps that's what you're seeing?
i'm just shooting from the hip here but you could try clearing all inputs before navigating.
i am using MS asp.net 3.5 chart control (Pyramid) and on the click of the series/datapoint i need to open a URL in a new window, something like javascript window.open.
Now i have tried a hell lot but that doesn't work. I am not able to give javascript to the datapoint.
Secondly i got to know that MapAreaAttributes could be given to Series as mentioned below if a new window needs to be open
series.MapAreaAttributes= "target='_blank'";
But even this doesn't works????
Guide me! Thanks
I had your same problem just now.
Here is the solutions and it works:
Notice in your code that you're using the single quotation ('). It seems that this is not allowed by the chart control or something. Let me give you an example that might help you understand:
Let's assume you have a JavaScript function that opens a window showing some data when the user clicks on a column (point) in your data (series). You can do it like this:
Chart1.Series["MySeries"].Points[0].Url = "javascript:void(0)"; //this is just to tell the browser not follow a URL, since you will control this with your javascript
Chart1.Series["MySeries"].Points[0].MapAreaAttributes = "onclick=\"OpenWindow();\""; //this is to set the onclick attribute to fire your javascript function when the user clicks your column.
In the above example in the second line of code, notice that I have used double quotation instead of single ones. If you wrote it like this :
"onclick=\'OpenWindow();\'";
it will never work! You have to use double quotations...
Also, since I am a C# developer, you have to use the \" code to write double quotations otherwise, you will get compiler error.
I hope this helps!
I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.
How can I build my own search function for the webbrowser control, and if possible remove the built-in one (CTRL + F find function, that is)? Any help regarding this would be appreciated.
Edit - How would I search for text and select it? Pretty much like how the find dialog will find text and select it, I want to do that and I have messed with the document property of the page but I still can't get it to work. Any help?
That is the purpose of the WebBrowser control.
There are third party browser controls you can use instead:
GeckoFX
Mono.WebBrowser
Update (following comments):
You can capture the KeyPress events of the control to find out if ctrl+f was pressed, then use your own search algorithm on the document returned by the control.
I expect you should be able to do something similar with the third party controls as well.
We are using the WebBrowser control in c# winforms and need to be able to get information about the Url the cursor is positioned on.
So we have a web page in design mode, which has multiple urls, when the cursor is over one I would like to call a method which would return the id of the link.
Thanks
You can use the IHTMLCaret to get the cursor position from there using IMarkupPointer you can get the element in the current scope.
The webBrowser control has a Document property which has a Links collection. Each Link is an HTMLElement which has events you can tap into. Again, I'm not sure what you mean "cursor" because in the web world, unless if you're in a textbox, there really isn't a "cursor" (which is what I meant to ask in my comment) but you can tap into the MouseOver event and other stuff like that.
Example:
foreach (HtmlElement element in this.webBrowser1.Document.Links)
{
element.MouseOver += (o, ex) =>
{
Console.WriteLine(ex.ToElement.GetAttribute("HREF"));
};
}
This will print out the actual URL that the mouse is over.
You can have a look at this article - Hosting a web browser component in a C# winform - which explains several ways to perform that. or go directly to this one - Hosting a webpage inside a Windows Form - Basically what you need to do is handle the Click of the DOM object inside the COM WebBrowser of IE. You achieve this by handling the Js events inside your C# code.
I remember this kind of customization must be done using the AxSHDocVw.AxWebBrowser COM object instead of the System.Windows.Forms.WebBrowser Class from the newer versions of the .Net Framework.
I could send you more data about this, I did it some project, just give me time to find it ;). In the mean time try with those links.
By!