WebBrowser control: click won't work - c#

I used the DocumentComplete event to AutoComplete a form. Everything is OK except the checkbox. The html code is the following:
<span class="myClass" style="padding-left: 12px; vertical-align: bottom; cursor: pointer;">
<input id="ich_liebe_dich" type="checkbox" name="ich$liebe$dich">
<label for="ich_liebe_dich"> MyLabel</label>
</span>
I tried using:
webbrowser.Document.GetElementById("ich_liebe_dich").InvokeMember("click");
and
webbrowser.Document.GetElementById("ich$liebe$dich").InvokeMember("click");
and also:
foreach (HtmlElement current in webbrowser.Document.GetElementsByTagName(tag))
{
if (current.GetAttribute(attr).Equals(attName))
current.InvokeMember(invoke);
}
where attr="id", tag="input", invoke="click" and attName= either "ich_liebe_dich" or "ich$liebe$dich".
The best I got was a transiently - just for a fraction of a second - checked checkbox. Why would this happen? Any solutions?

leppie's answer made me curious because I've never read anywhere about InvokeMember("check") and I googled it! The first answer I got is this http://social.msdn.microsoft.com/forums/en-US/winforms/thread/750b11dc-08f8-4cb4-bcaf-80c91f0fd425/
I read the article and found a solution...
If I add this line on DocumentCompleted event then everything works ok!
if (webbrowser.ReadyState==WebBrowserReadyState.Complete)
It seems that the page has frames and the DocumentCompleted event fires before the whole page is loaded.
edit: I forgot to mention that the code I used (and works) is the following:
webbrowser.Document.GetElementById("ich_liebe_dich").InvokeMember("click");

I had already answered a similar question
webBrowser.Navigate("http://www.google.com");
if you have id use this:
webBrowser1.Document.GetElementById("id").InvokeMember("click");
if you have tagname use this
webBrowser.Navigate("http://www.google.com");
In Web Browser DocumentCompleted event
HtmlElement textElement = webBrowser.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "your text to search");
HtmlElement btnElement = webBrowser.Document.All.GetElementsByName("btnG")[0];
btnElement.InvokeMember("click");
if you have name class use this:
HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("className") == "button")
{
element.InvokeMember("click");
}
}
for add text in textbox google.com use this:
webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";

Related

Click on the Checkbox inside the Label or Div

I want to create the two methods, one for clicking on the specific checkbox and second for de-selecting the same checkbox.
I was trying to using the XPath and ID but unable to do so.
Please tell me how it can be done. Here is the HTML:
IJavaScriptExecutor js = (IJavaScriptExecutor)_driver;
js.ExecuteScript("window.scrollBy(200,document.body.scrollHeight)", "");
var mileageTextbox = driver.FindElement(By.Id("VehicleMileageMax"));
mileageTextbox.SendKeys("9500");
var checkBox = driver.FindElement(By.XPath("//label[text()='financial-check-all-8']::input[#type='checkbox'"));
Thread.Sleep(2000);
checkBox.Click();
Element is not visible
HTML code :
<input id="financial-check-all-8" type="checkbox" selected="filter.isDisabled" ng-model="filter.isDisabled" ng-change="vm.emptyFilterValue(filter);" class="ng-pristine ng-untouched ng-valid ng-empty" aria-invalid="false" tabindex="44"> <label for="financial-check-all-8" id="enabled-8" class="form-checkbox-label"></label>
You can try with this code :
//input[starts-with(#id,'financial-check-all') and #ng-model='filter.isDisabled']
Code :
var checkBox = driver.FindElement(By.XPath("//input[starts-with(#id,'financial-check-all') and #ng-model='filter.isDisabled']"));
checkBox.Click();
EDIT :
As per the Shared HTML you can use this xpath:
try to wait also before clicking on it, Just try with Thread.Sleep(4000); , If that works then we can easily replace that with webdriverwait.
//label[#for='VehicleMileageMax']/../preceding-sibling::div/descendant::input
or with this xpath :
//label[#for='VehicleMileageMax']/../preceding-sibling::div/descendant::label

C# Forms -> Click button in webBrowser

I'm trying to programmatically click a button in a iframe form in webexplorer. The button is a bit nasty though, as its coded not to always be active. The HTML for the button is:
<input disabled="" class="submit ui-button ui-widget ui-state-default ui-corner-all ui-button-disabled ui-state-disabled" id="btnEntryAddSav" role="button" aria-disabled="true" type="submit" jQuery1418076056597="6" value="Add ->"/>
I've tried using invoke but not having any luck
HtmlElement ADD = frame.Document.GetElementById("btnEntryAddSav");
ADD.InvokeMember("Click");
It just doesn't seem to actually click. I can see the button highlighted, but nadda happens. Any thoughts?
//in chrome
try
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement submit = doc.GetElementById("btnEntryAddSav");
submit.InvokeMember("click");
}
catch { }
//in IE try to find name tag and:
try
{
HtmlElementCollection Bpic = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement bp in Bpic) {
string name = bp.Name;
if (name == "input_name")
bp.InvokeMember("click");
}
}
catch{}

How radio button click if included onclick function?

<asp: label input name="id" value="p" onclick="form.submit();
return false;" type="radio">personal / romance
/label>
I am trying below both code but not getting any solution. Please if anyone have any good solution then it's really good help for me.
webBrowser1.Document.GetElementById("id").SetAttribute("value", "P");
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("label"))
{
if (el.InnerText == ("personal romance"))
{
el.InvokeMember("Click");
}
}
Your code looks okay, but the InnerText of the label is not personal romance
Try it like this:
string searchString = #"personal / romance";
if(el.InnerText.Equals(searchString)
{
el.InvokeMember("click");
}
Check when expect this element if the correct tag name is label. I can tell you for sure that this is not asp:label. You can check with Inspect Elements in Chrome, if the tag name is different use correct one when you are taking webBrowser1.Document.GetElementsByTagName(correct tag)

How can I make elements visible in C# Webbrowser?

HTML code (example):
<div class="clsA"><div class="clsA1">TEXT</div></div> <div class="clsB"></div>
I want make DIV which have class-attribute is clsA and their child-elements visible.
(both clsA and clsA1 appear).
I try
foreach (HtmlElement pageElement in webBrowser1.Document.All)
{
if (pageElement.GetAttribute("className").ToLower() != "clsa")
pageElement.Style = "display:none";
}
but everything disappear.
Thanks
The uppercase A in "clsA" will never match a to lower string, so you will set every item on the page to display:none.

How do I programmatically click on a div in a web browser?

I have been trying for a few days now and still have had no success in programmatically clicking on this div. All of the other input fields and buttons are working fine using InvokeMember("click") and RaiseEvent("onclick"), but I am unable to click on the following div:
<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>
This div is repeated several times on a page, but I just want to click on the first occurrence.
This is what I have done so far:
HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("class").Contains("pump request"))//also this condition is not returning true
{
e2.RaiseEvent("onclick");
}
}
#bleepzter
what if "somecontrol" is a class of the div instead of div's id?
since in my case i have div class "pump request" so (if i write "pump request" as somecontrol in above example) it return me Null in someDiv
<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>
#Cameron
yep i did entered the break; but the problem is the if condition never returns true so
HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("class").Contains("pump request"))//--> This condition is not returning true
{
e2.RaiseEvent("onclick");
break;
}
}
#Ilya Kogan
yea i just did a watch on e2.GetAttribute("class") and the weird thing happened that being reading the actual div (which i want to click) the value of class was empty :-o
try this one
if (e2.GetAttribute("className").Contains("pump request"))
{
e2.InvokeMember("Click");
}
you can try this piece of code by using a web browser control.
// browser is the web browser control
HtmlElementCollection col = browser.Document.GetElementsByTagName("div");
foreach (HtmlElement helemnt in col)
{
if (helemnt.InnerText !=null && helemnt.InnerText=="something")
{
helemnt.InvokeMember("Click");
break; // break the loop
}
}
It is simple. Here is an example which assumes your browser control is called browser, and the div you are looking for is called somecontrol (i.e. the id of the div is somecontrol):
HtmlElement someDiv = browser.Document.All["somecontrol"];
object someDivElement = someDiv.DomElement;
MethodInfo clickMethod = someDivElement.GetType().GetMethod("click");
clickMethod.Invoke(someDivElement, null);
All this is possible via reflection.

Categories