How to click browser and input into textbox? - c#

How I can click buttons with ids or anything other, input to text box string etc. I know in windows form is easy with getelementbyid. But in WPF I cant find anything. I know how get source to string but I cant make click. Any ideas how do this or is even possible? I can get list of Ids from source +regex. Or is there too something I can get simply list? Need something like this:
HtmlElement button = webBrowser1.Document.GetElementById("lButtonSearch");
button.Click += new HtmlElementEventHandler(GotoSearchPage);
I can do something like this but what next, how display it?
System.Windows.Forms.WebBrowser weba = newSystem.Windows.Forms.WebBrowser();
weba.Navigate(new Uri("www.google.com"));
string testowo = "btnI";
System.Windows.Forms.HtmlElement htmlElement = weba.Document.GetElementById(testowo);
htmlElement.InvokeMember("click");
How now convert it to display lets say WebBrowser id is =browserwindows
browserwindow=weba
wont work

Try this:
var doc = webBrowser1.Document as IHTMLDocument2;
var button = doc.all.OfType<IHTMLInputElement>().FirstOrDefault(b => b.name == "btnG");
if(button != null)
{
((IHTMLElement)button).click();
}

Related

Need to enter Text

I tried finding the element using Get but it does not work, thats why i treid with GetElement method
I am trying to enter text in an textbox element found using GetElement in teststack white using C#
i want to know how to cast the automation element to UIitem so that i can do enter() or click operation on that element
var all = appWindow.GetElement(SearchCriteria.ByControlType(ControlType.ComboBox)
.AndByText("Model collapsed"));
var element = all.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Edit Box collapsed"));
element.enter("");
when i do element.enter or click it gives error, i think i need to cast it or is there any other way where i can achieve this. Thank you.
After using the below code i was able to enter text.
var all = appWindow.GetElement(SearchCriteria.ByControlType(ControlType.ComboBox)
.AndByText(parentValue));
var element = all.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, childValue));
TextBox textBox = new TextBox(all, appWindow.ActionListener);
TestStack.White.InputDevices.AttachedKeyboard keyboard = appWindow.Keyboard;
textBox .Click();
keyboard.Enter("test");

Get Media ID dynamically

I am currently working in a project with c# and umbraco CMS. And now im facing some issues one of them is that I don't know how to get the media ID dynamically, please take a look ID COMES FROM UMBRACO
Media file = new Media(3557);
string url = file.getProperty("umbracoFile").Value.ToString();
string teste = file.getProperty("impressions").Value.ToString();
if (teste == "" || teste == null) { teste = "0"; }
int count= Convert.ToInt32(teste);
file.getProperty("impressions").Value = count+1;
file.Save();
Do you see that 1st line ? Media file= new media(id)? I want to get this id dynamically and I will explain why. I have this handler in order to get the banner clicks on the site. I have 4 images and I want to have a count for how many times the client clicks on them. So for that I can't have the id = 3557 , I need to get the id of the image dynamically.
I assume you are using an event handler, something like:
protected void imgMyMedia_OnClicked(object sender, EventArgs e) { your code }
In that case, you can cast the sender object to your control type, and read any parameter set there. I recon it would be something like this:
MediaControl myMedia = (MediaControl)sender;
int ID = myMedia.MediaId;
or something similar. I am not familiar with the umbraco code and can not be as precise, but this should be about right.
When you render the image in the first place you want to put the image id in a data attribute so your html will be something like:
<img src="/media/someimage.jpg" data-id="someMediaIdHere" alt="alt" title="title" />
Then use that when your javascript picks up the onclick event, then you can pass the image id to the server side and do your tracking.

Edit a button by its given name

I am generating x amount of buttons and I give all of them a unique name.
After all those are generated, I want to edit one of them without regenerating them so I was wondering if I could get a component by its name?
I am using WinForms
Yes:
Control myControl = Controls.Find("textBox1");
Now, beware that you have to do proper casting hen found, because Find returns a control.
You can use Controls property of your form (or some container control on your form). With LINQ you can select buttons and then find first button with required name:
var button1 = Controls.OfType<Button>().FirstOrDefault(b => b.Name == "button1");
Or if you want to search child controls recursively
var button1 = Controls.Find("button1", true)
.OfType<Button>()
.FirstOrDefault();
Without LINQ you can use method Find(string key, bool searchAllChildren) of ControlCollection:
Control[] controls = Controls.Find("button1", true);
if (controls.Length > 0)
{
Button button1 = controls[0] as Button;
}
Button btn1 = (Button)(Controls.Find("btnName"));
This will get the required button and will save the button attributes into a new Button btn1
After all those are generated, I want to edit one of them without
regenerating them so I was wondering if I could get a component by its
name?
var myButton = Controls.Find("buttonName", true).FirstOrDefault(); //Gets control by name
if(myButton != null)
{
if (myButton.GetType() == typeof(Button)) //Check if selected control is of type Button
{
//Edit button here...
}
else
{
//Control isn't a button
}
}
else
{
//Control not found.
}
Make sure you add a reference to: linq.

Select one string line in a list

I'm trying to simply select one string line out a long list strings that are held on a server and seperated with a pipe character. This string is grabbed by a php script and the string line is a list of all the media and folders I have on my server.
In my code I'm getting this information and returning it with the following code:
using (var client = new WebClient())
{
result = client.DownloadString("http://server.foo.com/images/getDirectoryList.php");
}
textBox1.Text = string.Join(Environment.NewLine, result.Split('|'));
And it looks like this:
But when I try to simply click on one of them, my cursor simply just goes to where I've clicked. Like this, I tried to select md-harrier.jpg and my cursor just ends up at the end of jpg:
What I'm really wanting is pictured below. I click on Koala.jpg and the whole thing is highlighted and I have the ability to store the name of what it is I've just clicked on. TO achieve that screen shot I had to click next to Koala.jpg and then drag my mouse along.
Is there anyway I can achieve what I want to achieve?
The key thing to note about this is that I will have no idea how many files will be on the server, nor what they will be called. My php script is grabbing this information and displaying it in my winform text box using the code I have wrote above.
as Simon said you need a ListBox, a ListBox fits here because it allows you to select a line, and you can register to the event of SelectedIndexChanged and store the name that was selected.
to initiate the values do
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
listBox1.Items.AddRange(result.Split('|'));
listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
and on the selectedItemChanged:
string currVal;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
currVal = (string)listBox1.SelectedItem;
}
As you said you have no reason to use TextBox,then by using ListBox you can achieve that in this way;
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
string[] names=result.Split('|');
foreach(string name in names)
{
if(name!="|"&&name!=" ")
{
listbox.Items.Add(name);
}
}
Additionally,if you would like to store selected item in a variable subscribe to ListBox's SelectionChangedEvent and store the selection index in a variable in this way;
int selection=;
private void ListBox1_SelectionIndexChanged(object sender,EventArgs e)
{
selection=ListBox1.SelectedIndex;
}

How to programmatically click on text input in WebBrowser using C#

I have opened a website using WebBrowser. Now I would like to programmatically click input text (textbox) field. I can not use focus because this website uses JS to unlock this field only if it's clicked and I've tried also this:
Object obj = ele.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);
But it returns mi = null. How to do this so it will work?
Very similar to my answer on your other question.
Get an HtmlElement respresentative of your textbox, and call HtmlElement.InvokeMember("click") on it.
If you can, use:
webbrowser1.Navigate("javascript:document.forms[0].submit()")
or something similar. For me, it's been much easier and more accurate.
To fill-up a text field on a webpage:
string code ="";
code = code + "var MyVar=document.getElementById('tbxFieldNameOnWebPage');if(MyVar != null) MyVar.value = 'SOMEVALUE';";
domDocument.parentWindow.execScript(code, "JScript");
Then To Click a button on a webpage:
code = "";
code = "var SignupFree = document.getElementsByTagName('button')[1];";
code = (code + " SignupFree.click();");
domDocument.parentWindow.execScript(code, "JScript");
you can also use document.getElementById('buttonID'); instead of document.getElementsByTagName('button')[1]; but an id must be provided for this button on that particular webpage.
Use InvokeMethhod on HtmlElement or Browser.InvokeScript function.

Categories

Resources