C# Forms -> Click button in webBrowser - c#

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{}

Related

C# Target HTML button by class

I want to click on a button which doenst have an ID. I used this stackoverflow question to get started, but I can't seem to make it work.
//Load page
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("/login");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
//Log in
HtmlDocument doc = webBrowser1.Document;
doc.GetElementById("username").SetAttribute("Value", "--");
doc.GetElementById("password").SetAttribute("Value", "--");
doc.GetElementById("_submit").InvokeMember("Click");
WebBrowser webBrowser2 = new WebBrowser();
webBrowser2.Navigate("/Start");
while (webBrowser2.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
var buttons = webBrowser2.Document.GetElementsByTagName("button");
Console.WriteLine(buttons); **//Output here: 'System.Windows.Forms.HtmlElementCollection'**
foreach (HtmlElement button in buttons)
{
if (button.InnerText == "Filter ")
{
button.InvokeMember("Click");
Console.WriteLine("Clicked!"); **// This will never get writing into the console.**
}
}
}
The HTML:
<button class="down-arrow-btn" type="button" id="toggleFilterFieldsButton"
title="Overige filtervelden tonen/verbergen"> ▼
</button>
<button class="filter-btn" type="submit" name="filter_action" title="Filteren"
style="color: #7989a0"
value="filter">Filter
</button>
<button class="herstel-btn" type="submit" name="filter_action" title="Filteren opheffen, alles tonen"
value="reset">Herstel
</button>
In this case I want to press the second button.
Edit:
First solution:
var element = webBrowser2.Document.GetElementsByTagName("button")
.Cast<HtmlElement>()
Where(e => !String.IsNullOrEmpty(e.GetAttribute("name")) && e.GetAttribute("name") == "filter_action")
.FirstOrDefault();
Second solution:
var elementToClick = webBrowser2.Document
.GetElementsByTagName("button")
.Cast<HtmlElement>()
.FirstOrDefault(m => m.GetAttribute("className") == "filter-btn");
elementToClick.InvokeMember("Click");
Console.WriteLine(elementToClick);
I have tried multiple other solutions. But the htmlElement is always null.
I think the problem is due to the website being pretty secured. And the button I am targeting is not visible for the program? Even tho it's visible in the code.
If I copy the whole HTML code (view-source:https:// url ) and make it an local .html, it works fine. (all of the 3 solutions.)
Using only 1 web browser control did the trick.

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

How do I click a button that has the same class as 10 others?

I have a webpage that I'm trying to click a button on.
This is a couple of the webpage button codes:
<div class="ContentTab">My Bets</div>
<div class="ContentTab">Chat</div>
<div class="ContentTab">Account</div>
It only has the innertext different on them.
My goal was to click the "Chat" tab with this code, but it doesn't work.
foreach (HtmlElement chat in wb.Document.GetElementsByTagName("input"))
{
if (chat.InnerText == "Chat")
{
chat.InvokeMember("Click");
loggedIn = true;
break;
}
}
Does anyone know how to do it correctly?
Thanks
You are searching for input tags with this code:
foreach (HtmlElement chat in wb.Document.GetElementsByTagName("input"))
But all your elements are declared as div:
<div class="ContentTab">My Bets</div>
<div class="ContentTab">Chat</div>
<div class="ContentTab">Account</div>
Making these consistent will fix your problem.

WebBrowser control: click won't work

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";

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