How radio button click if included onclick function? - c#

<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)

Related

grab value of a tag text using geckoWebBrowser

Check the code bellow. I am working on a c# winform application. There i am using geckofx geckoWebBrowser to get some html values. From following html i want to grab text- Super Deluxe Round Silver Above Ground Winter Pool Cover but you can see what i have already tried to grab that text and that not works. Any idea whats wrong i am doing? How to fix?
c#:
url = #"https://www.homedepot.com/s/0723815359971";
geckoWebBrowser1.Navigate(url);
DateTime now = DateTime.Now;
do
{
this.Refresh();
Application.DoEvents();
} while (now.AddMilliseconds(5000) > DateTime.Now);
GeckoHtmlElement element = null;
var geckoDomElement = geckoWebBrowser1.Document.DocumentElement;
if (geckoDomElement is GeckoHtmlElement)
{
element = (GeckoHtmlElement)geckoDomElement;
innerHtml = element.InnerHtml;
title = element.GetElementsByTagName("pod-plp__brand-name")[1].NodeValue;//this is what already tried but not works
if (title != "")
{
MessageBox.Show(title);
}
}
Html:
<a class="" data-pos="0" data-request-type="sr" data-pod-type="pr" href="/p/Swimline-16-ft-x-16-ft-Round-Silver-Above-Ground-Super-Deluxe-Winter-Pool-Cover-SD12RD/305609609">
<span class="pod-plp__brand-name">Swimline</span>
Super Deluxe Round Silver Above Ground Winter Pool Cover
</a>
GetElementsByTagName gets elements by the HTML tab name. (eg. a, span, div etc.)
Something like this ought to do it (assuming single 'a' in your document):
element.GetElementsByTagName("a")[0].FirstChild.NextSibling
Which gets the second element in the 'a', if that what you want.

C# GeckoFX Browser - How to click/set value to XPath?

Im having problems with website automation, I am able to confirm if website contains XPath im looking for, but I have no idea how to make program to set text or click it.
Im using Firefox 22.0 / Xulrunner 22.0
Code I used to verify if website contains XPath:
GeckoNode element = gWB.Document.GetSingleElement(x.ToString());
if (element != null)
{
//What here to make program to click/fill XPath?
}
Thanks
To perform a click, cast the GeckoNode type to GeckoHtmlElement then you can call the click method.
GeckoHtmlElement element = (GeckoHtmlElement)gWB.Document.GetSingleElement(x.ToString());
if (element != null)
{
element.Click();
}
Setting the value can be different depending on the type of the element, but you can try and use attributes such as NodeValue, TextContent, and InnerHtml.
If you want to use XPath, you can try with this:
browser.LoadXml("<MyTag><div>helloworld</div></MyTag>");
var r = browser.Document.EvaluateXPath("//div");
Assert.AreEqual(1, r.GetNodes().Count());
so in prev code:
GeckoElementCollection nodes = browser.Document.EvaluateXPath(x.ToString()).GetNodes();
foreach(GeckoNode node in nodes)
{
//do whatever you need to do with the node ..
GeckoElement element = node as GeckoElement;
element.click();
//..
}

How do I check the display styling of an element with Watin?

I'd like to do the following with watin. Maybe you guys know the best ways to go about doing this. I've been unable to find any thorough documentation on watin's power, I've even checked the homesite with little luck.
I'd like to...
Check the visibility/display value of a div.
Check the class name of an element.
eg:
Span this_span = Span(Find.ByText("Loading..."));
if (this_span.Visibility=="True")
{
}
and
if (this_span.Class.Value=="classname")
{
}
I appreciate any help!
if (this_span.Style.Display != "none")
{
}
Check the class name of an element.
if (this_span.ClassName=="classname")
{
}
Check the class name of an element.
if (this_span.Style.GetAttributeValue("display")!="none")
{
}

aspx change focus from different TextBoxes on the fly

I am trying to change the focus from one textbox to another while the character count in
on textbox reaches 13 I am using the code below with nothing happening whatsoever:
if (!this.ClientScript.IsClientScriptBlockRegistered("qtyFocus"))
{
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "qtyFocus",
#"<script type='text/javascript' language='javascript'>function qtyFocus(){
var trckNumberLength = document.getElementById('txtTrackingNumber').value.length;
if(trckNumberLength == 13){
document.getElementById('txtQuantity').focus();
}}</script>");
}
txtTrackingNumber.Attributes.Add("onchange", "javascript:return qtyFocus();");
can anyone help please ?
Probably because the line in the script that's doing
var trckNumberLength = document.getElementById('txtTrackingNumber').value.length;
Needs to be changed for:
var trckNumberLength = document.getElementById('"+txtTrackingNumber.ClientID+"').value.length;
The reason being that txtTrackingNumber will very likely have a different Id when it's rendered on the page so you need to use the ClientID property of the control instead of the id you defined on the markup.

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