How getAttribute("onclick") - c#

For example I have this html code:
<a href="#" onclick="return Index.submit_login('server_pl80');">
<span class="world_button_active">Świat 80</span>
</a>
And I need to get attribute of onclick, because I may get more links and I must find difference between them. For my opinion get attribute of onclick is only one way.
But if I GetAttribute("onclick") from HTMLElement it will return System.__ComObject.
Is there any idea how read onclick value from webBrowser?

I have only this:
HtmlElement selected_div = #webBrowser1.Document.GetElementById("div_id").GetElementsByTagName("div")[0];
HtmlElement a = selected_div.GetElementsByTagName("a")[0];
string rightLink = a.GetAttribute("href");
string onclickLink = a.GetAttribute("onclick"); // return "System.__ComObject" string
if (rightLink == "http://www.example.com/#")
a.InvokeMember("click", null);
"onclick" or "onClick" in getAttribute(); doesn't make a difference
This code works but click on first link on the list of servers.. I need choose the server and links have difference only in onclick attribute.

Related

Programmatically click button in C#

I'm trying to programmatically click a series of buttons on an HTML web page which looks as follows:
<div class="srp-actions blue-button"><a class="primary-action-button label" href="/people/invite?from=profile&key=243930744&firstName=Will&lastName=Yang&authToken=p8Oz&authType=OUT_OF_NETWORK&connectionParam=member_desktop_search_people-vertical-module&csrfToken=ajax%3A7824954558998584370&trk=vsrp_people_res_pri_act&trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary" data-li-result-interaction="instant-connect" data-li-success-text="Invite Sent" data-li-connect-href="/people/contacts-search-invite-submit?memIds=243930744&authTokens=p8Oz&authTypes=OUT_OF_NETWORK&from=voltron&firstName=Will&lastName=Yang&isAjax=true&connectionParam=member_desktop_search_people-vertical-module&csrfToken=ajax%3A7824954558998584370&trk=vsrp_people_res_invite_act&trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Connect</a><div class="secondary-actions-trigger"><button role="button" class="trigger"><span>Secondary Actions</span></button><ul class="menu"><li>Send InMail</li><li>Share</li></ul></div></div>
Here's the current code to find the button element and perform the action:
HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("a");
// First find and click "Connect" buttons
foreach (HtmlElement item in elements)
{
if (item.OuterHtml.Contains("action-button label") &&
!item.OuterHtml.Contains("Message") &&
item.OuterHtml.Contains("OUT_OF_NETWORK"))
{
item.SetAttribute("href", item.GetAttribute("data-li-connect-href"));
item.InvokeMember("Click");
}
}
The code properly find the anchor element, but the InvokeMember method doesn't seem to yield any result, any idea what is wrong?
The tag you are using does not specify exactly which specific tag you need:
x = webBrowser1.Document.GetElementsByTagName("a")
Instead try :
x= webBrowser1.Document.GetElementsById("anchor_id");
x.InvokeMember("click");
Or try using following method to verify whether its the intended tag you're using via attribute.
if (element.GetAttribute(attribute).Equals(attName))

Web browser control to fill <input> field

I'm trying to open a web page in my webbrowser control and change the value of input fields. Works good when I'm doing it like this webBrowser.Document.GetElementById("Email").SetAttribute("value", "example#example.com");
on a page with defined element Ids, but now I've encountered a page where the html/javascript looks something like this:
<input id="${Id}" name="${Id}" type="text" class="text field" value="${Value}" title="${ToolTip}" />
So my question is how do I find this specific input field from the C# code?
Try This
This is a C# coding
HtmlElement Elem = AutomationWebBrowser.Document.GetElementById('<your element ID>');
Elem.SetAttribute("value", '<value to assign in input control>');
Also you can use variables inside the GetElementById and SetAttribute function
You can find element by class name by using the following function,
public static HtmlElement GetHTMLElementByClass(HtmlDocument document, String className)
{
foreach (HtmlElement element in document.All)
{
if (element.GetAttribute("className") == className)
{
return element;
}
}
}

Invoke button click programmatically

I want to programmatically click button on a webpage with source like this
<input alt="BusiBtn" class="aButtn" type="submit" value="Search" tabindex="16">
When I do
WebBrowser b = new WebBrowser();
b.Navigate(URL);
while (b.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
b.Document.GetElementByID("BusiBtn").InvokeMember("click");
I get "Object reference not set to an instance of object error".
Can somebody help.
Thanks
Rashmi
What you can do in this case simply find all the HtmlElements having input tag. If you need to invoke all the input tags in general, then just invoke click on them. And if you need only the above input element, then filter all the input tags to search for the specific tag with the attribute values like above. Please have a look at the following code:
HtmlElementCollection elems = b.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
string altStr = elem.GetAttribute("alt");
string classStr = elem.GetAttribute("class");
string typeStr = elem.GetAttribute("type");
string valueStr = elem.GetAttribute("value");
string tabindexStr = elem.GetAttribute("tabindex");
if((altStr == "BusiBtn") && (classStr == "aButtn") && (typeStr == "submit") && (valueStr == "Search") && (tabindexStr == "16"))
{
elem.InvokeMember("click");
break;
}
}
You're using the wrong field.
alt is for alternative text.
You have not actually given that button an id of BusiBtn.
Try:
<input id="BusiBtn" class="aButtn" type="submit" value="Search" tabindex="16">
The clue is in the GetElementByID call. It's not called GetElementByAlt for a reason ;)
add 'name' property to input tag and then use GetElementsByName property
You should use the GetElementsByTagName method instead of GetElementById to get all Input-Elements on the page and then cycle through using GetAttribute. An example can be found here http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname(v=vs.110).aspx.

check the url of current page in if statement

so i have this button that appears on multiple pages all leading to the parent page. But now - when i reach the parent page i still have the button showing. I want to remove/hide the button when on the parent page.
Is it possible that i could check my current page url and check to see if it the parent page?
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
thinking using an if statement surrounding the tag
If you wanted to do your check in the code behind page, you can always execute the code below during the page load event.
if (Path.GetFileName(Request.Url.LocalPath) == "ParentPage.aspx")
{ element.visible = false; }
else
{ element.visible = true; }
yeah that's what the window.location variable is for see this
When you are in your parent page, what is the ElementId value ?
I assume it can be null, so you may be able to check this value before displaying your button
#if(ViewBag.ElementId != null){
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
}
else { //TODO...
}
It looks like ASP.NET MVC to me.
#if (!Request.Url.AbsolutePath.EndsWith("/i/dont/want/this"))
{
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
}

Click on element in Webbrowser

I have about 10 of these tags under each other:
<a class="answer" href="#">This is one</a>
<a class="answer" href="#">This is Two</a>
<a class="answer" href="#">This is Three</a>
Now I need my webbrowser in C# to click on <a class="answer" href="#">This is Two</a>
How would I do this? Only got text and a tag with a class. No value this time.
Thanks
If you want it just for that "This is Two" link I believe it would go something like this:
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("This is Two"))
link.InvokeMember("Click");
}

Categories

Resources