Web Browser control click - c#

I am using Web Browser control in my project and i open a web that have file chooser object.
Now i want to inject programmatically a path to a file in this file chooser object .
I tried to get the HtmlElement ,but i not found in the source code the element Id.
Edit:
In the web there is "Browse..." button that open a file chooser,and then the file path is show in little text filed, and i want to inject the file path my self to the text field.

First.
establish a name, id or class for the element you want to change the value of. There are a number of ways of doing this. I would use firebug for firefox, or IE developer toolbar, or just view the source of the page and establish what the name/Id of the field is. Now if that field does not have a Name or an ID that you can use you might be able to get the containing element and iterate through the Child elements n times until you get the element you want.
For my embedded browser app I did the following in c#:-
// webBrowser is the name of the embedded IE browser in your app
var htmlDocument = webBrowser.Document;
if(htmlDocment!=null)
{
var field = htmlDocument.GetElementById("...the id...");
if(field!=null)
{
field.SetAttribute("value","...yourfilenamepathonyourmachine...");
}
// Now you would need to establish the ID of the submit element and click that
var submitButton = htmlDocument.GetElementById("...submit button...");
if(submitButton!=null)
{
submitButton.InvokeMember("Click");
}
// your code to loop?
}
So do you have the HTML of the page in question? that might help and a better answer can be given
Regards Julian

Related

How to get the clicked element using webview2 in windows forms

I have a query in windows. forms I am new to this.
I have developed a form where users can open any website from it and upon right click of any element I am displaying the element name, id, and few attribute values in a data grid. For this, I have used webbrowser control.
However, I was facing some errors for a few of the sites so I tried to move to webview2. But here comes the issue
Earlier I used to get the element using the below code
HtmlElement element = webbrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
But now I am unable to retrieve an element by using webview2.
Can someone please help me with this?
You will need to use JavaScript. You will need async methods.
Then, you can get the element by passing a javeScript string.
Point p = e.ClientMousePosition;
string jElement = await webBrowser1.ExecuteScriptAsync($"document.elementFromPoint({p.X},{p.Y})");
The result is JSON. You will need to parse the result to get the element name.
I am trying to figure out the same thing.

C# : upload file to a file host

i have been trying to upload a file to Fileshawk.com from my application.
Here is how i did it :
1- Created a web browser control
2- Loaded the page
3- in the web page it has an input tag of file.
4- i tried to add the file to HTML by using that method which failed.
private void Set_Text_TAG_INPUT(string attribute, string attname, string value)
{
// Get a collection of all the tags with name "input";
HtmlElementCollection htmle = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement current in htmle)
{
if (current.GetAttribute(attribute).Equals(attname))
{
current.SetAttribute("value", value);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Set_Text_TAG_INPUT("id","upfile_1342028154587", "FILE.txt");
}
So it didn`t change the value of the Input tag.
Is there any way to add my file to the HTML or page code and about pressing upload i already have a method that invokes the web browser button and click it.
For example , when you click Select file from the input file tag , a window pop up and you select the file , now when you click okay , where is that file stored in the HTML ?
Finally excuse me if i am a newbie in HTML and web knowledge.
This is a security feature in browsers. You cannot programmatically set the value of the value of an input tag of type 'file' since it would introduce a security hole (e.g. a malicious developer could set the value right before handling a submit event and get whatever file they wanted).
Use WebScraper, a C# .NET library to upload file/download file easily, but you need to analyze Fileshawk.com HTML to come up with your own WebScraper syntax, there are totally 22 file host syntax available for sample and already useable.
http://sorainnosia.com/Home/Article/WEBSCRAPER

Modifying rich content on home page programmatically

I am provisioning site collections programmatically in SP2010. The process is working fine but I need to be able to customize some text on the home page that is contained in rich text (not in a web part). Is there a way to grab that HTML code and modify it from code? For instance, embedded within the home page, I have a title and a paragraph of text. I'd like to customize the title based on one of the input values provided from the provisioning request. Is it possible to do this? Any suggestions would be appreciated!
You can access the content area as follows:
PublishingWeb pw = null;
//"web" is your SPWeb, didn't include using statement for your SPSite/SPWeb in this sample
if (PublishingWeb.IsPublishingWeb(web))
{
pw = PublishingWeb.GetPublishingWeb(web);
}
//todo: add some handling here for if web is not a publishingWeb
//assuming your home page is the default, if not - get the correct page here
SPFile defaultpage = pw.DefaultPage;
if (!(defaultpage.CheckOutType == SPFile.SPCheckOutType.None))
{
//programatically creating this stuff, so cancel any unexpected checkouts
defaultpage.UndoCheckOut();
}
defaultpage.CheckOut();
//Field you want to add HTML in (Alternatively, retreive existing data and modify the HTML)
defaultpage.ListItemAllFields["PublishingPageContent"] = "string of HTML";
defaultpage.ListItemAllFields.Update();
defaultpage.CheckIn("My Comment");

How to click an element (hyperlink) on a webform from C#, when it does not have any ID and Name

For the last two weeks I have been kind of stuck on a problem.
I am developing some web scrapers using C# and I am using a WinForms WebBrowser control in my application. I am able to fill up the web form which is opened in my browser and submit it automatically by using the following code:
HtmlElement submitButton = document.GetElementById("Element_ID″);
submitButton.InvokeMember(“click”);
So far everything is fine, but the problem is that there is one another element in the web form that I want to click too, but this element does not have any id or name so I don't know how to click this one.
Please help me as soon as possible I need it for my master thesis.
(I want to click the next page arrow button in the give website:
http://www.gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW )
I've written many web-scrapers in the past using embedded WebBrowsers, so you've come to the right place.
When the element does not have a name you need to find it by either content, or another associated element that is named.
In the first instance we wrote helper methods to iterate the hierachy looking for a specific piece of content within an element.
For the second option you get the named element and use a specific index for the desired child.
A combination of both (find a specific parent then look for a child with the right content)
In your specific example webpage, the next page anchor has a class type of "arrow next" you can search for.
You could do
HtmlElement next_arrow = document.GetElementsByTagName("a")
.Cast<HtmlElement>()
.Where(e => e.GetAttribute("class") == "arrow next")
.FirstOrDefault();
if (next_arrow != null)
{
next_arrow.InvokeMember("click");
}
Here's a trick, not by InvokeMember("click") rather just "simulating the click" -
this is the link for the first page:
gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW
as you see page=0; clicking next, gives the link -
gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU/DMAyFf00vmzLFdprE8gkmwTggEENcp3RNxxh0o-pmA8euJBlO1G0-p-pvCf58zNwUzW-pDKyQalSmckExl6DqJpKnPCEuVbDaYFUvBcEIFXgVu1Ws2nV6Xac-pZn89X5xFwoed2MvQbmI73rf1eL4L3SakFFsJOBpnzcJbte9W4hSI-pQ/S912oY4qhWz5LDSC992Dl/QR60ahPki2OZKeNfCgiba18oicmLV8lTcoS8t6BJ8zsHMo3yEU1VE1D1ZmWm7Tt-psXxtNwCMmjS4BhJ7oDAy72WR5CH/MT0l1HQEVa46QDK2Z/JsTyhcdIAWrZeGy8/k7LJ5YQBAAA-e&page=1&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW
now page=1
and so on... in general clicking next means page=(x+1) clicking prev means page=(x-1). so build a string according the requirements. this addresses ur problem, however there are some other data also sent with querystring, that u have to append to the string as well.

Click an HTML link inside a WebBrowser Control

C# Visual Studio 2010
I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).
This is the content of the a href link:
<a
id="u_lp_id_58547"
href="javascript:void(0)"
class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">
My Assigned</a>
Is the anyway to click the link from C#?
Thanks!
UPDATE:
I feel like this is close but it is just not working:
HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());
I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?
Something like this should work:
HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")
EDIT:
Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
UPDATE:
You can get the links within an IFrame using:
webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");
Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.
In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.

Categories