How Can I Select a Drop Down Option on a Webpage? [duplicate] - c#

So I have a listbox I want to change, that looks like this:
How do I change the July value? I need this to be 100% automated and change it to January. I made a lot of accounts on various websites and need to change them all back to the same birthdate. Yes, I'm aware I'll have to find the ID of it, etc.

View the HTML of the website and identify the id and values of the dropdownlist, for example:
<select id="bdayMonthId" size="1" name="bdayMonth">
<option value="">Month</>
<option value="Jan">January</>
<option value="Feb">February</>
<option value="Mar">March</>
</select>
To pre-select the dropdownlist value in the WebBrowser control use this Winform code:
webBrowser1.Document.GetElementById("bdayMonthId").SetAttribute("value", "Feb");

I'm a bit confused about your question. If you just want to alter an element in the HTML, Jeremy's answer is the best, and simplest, way to go. If you wanted to call the document's javascript, this should work:
Let's assume that the webbrowser's document contains the following HTML:
<html>
<head>
<title>Invoke Test</title>
</head>
<body>
<div id="testdiv">Waiting...</div>
<script>
function changeDate(date) {
var x=document.getElementById("testdiv");
x.innerHTML = date;
}
</script>
</body>
</html>
To invoke the webbrowser document's javascript method, you can use something like this:
private void button1_Click(object sender, EventArgs e)
{
object o = webBrowser1.Document.InvokeScript("changeDate('june')");
}
No System.Web, ASP, ScriptManagers or Interop are needed. All the tools you need to control webbrowser and document objects and events come with the webbrowser control.

Related

How to trigger jquery ".change" event on dropdown select programatically c#

I'm trying to automate html events using c#.
I have html dropdown and on change of its value it shows/hides specific divs.
Below is sample HTML code.
<!DOCTYPE html>
<html>
<body>
<select id="ddl">
<option value="1" selected="selected">TextBox</option>
<option value="2">Button</option>
</select>
<input style="display: none" type="text" class="textboxclass" />
<div class="btn" style="display: none">
<button type="button">Submit</button>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$('#ddl').change(function()
{var name = $('#ddl option:selected').text();
var id = $('#ddl option:selected').val();
if(id =="1"){$('.textboxclass').show();$('.btn').hide();}
else{$('.textboxclass').hide();$('.btn').show();}});
</script>
</body>
</html>
I'm using mshtml.dll to read the HtmlDocument and I have to fire "change" event programmatically.
Tried different ways like below ones:
HTMLOptionElement drpelem.select=true; HTMLSelectElement
elem.click();
But it's not working. Please guide me.
I think this question was already asked and answered in a different form, here: https://stackoverflow.com/a/1456714/19020 or here: https://stackoverflow.com/a/11175179/19020 -- since mshtml is used by Internet Explorer.
But really, to load a full browser experience you'll need to load an ActiveX control, or COM control or whatever they're called by Microsoft nowadays. As far as I know, mshtml.dll just renders the HTML.

Get attributes of a Web Element

Is there any technique that can be used to get attributes of an element in a web page? I'm trying to get properties of elements such as Links, Buttons, check boxes and Radio Buttons for test automation purposes to make it more easier. But I couldn't find any solution. I heard that CODEDUI can be used. If so, how can I implement it?
As you have mentioned html tag in your question, one solution is using jQuery like this:
$( elemement ).attr( "attribute" )
you do not get property of html page with program language php and c# you should useing jquery and Similar libraries or JavaScript .
for this problem you must using this code in select html with jquery :
<select id="selectVehicle">
<option value="1" data-year="2011">Mazda</option>
<option value="2" data-year="2015">Honda</option>
<option value="3" data-year="2008">Mercedes</option>
<option value="4" data-year="2005">Toyota</option>
</select>
$("#selectVehicle").change(function () {
alert($(this).find(':selected').data("year"));
});
or get attribute of elements :
$(this).attr('data-fullText');

MVC Razor Dropdownlist with Image and label?

I was wondering is there a way to list image and label using #Html.DropDownListFor helper in MVC razor?
Something like this:
(source: dotnetspeaks.com)
There's nothing like this built into MVC, but I'm sure there are many 3rd-party components. I personally use dijit for this kind of thing, but since you have JQuery as one of your tags, you could probably use the Menu from JQuery UI.
DropDownListFor renders a <select>. This HTML element does not support an image in the list. you will need to use a JavaScript library to get this effect. several of the libraries use a <select> as the basis (they hide the select but get values form it.) there are dozens of select replacement libraries.
Have a look at this tutorial, this will help you creating drop down list with images.
look here. It is jquery, it is easy to implement with a few lines
<link rel="stylesheet" type="text/css" href="../Scripts/msdropdown/dd.css" />
<script type="text/javascript" src="../Scripts/msdropdown/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="../Scripts/msdropdown/js/jquery.dd.js"></script>
<div>
<select name="webmenu" id="webmenu">
<option value="calendar" title="~/Content/Images/arrow.png">Calendar</option>
<option value="shopping_cart" title="~/Content/Images/SendtoFriend.jpg">Shopping Cart</option>
<option value="cd" title="~/Content/Images/facebook.png">CD</option>
<option value="email" title="~/Content/Images/Email.png">Email</option>
<option value="faq" title="~/Content/Images/VotePositive.png">FAQ</option>
<option value="games" title="~/Content/Images/VoteNegative.png">Games</option>
</select>
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#webmenu").msDropDown();
});
</script>
In order to work. I have to changed a Little.. I prefer to use title to show the image. It Works to me

How do I change a dropdown box in a webbrowser control?

So I have a listbox I want to change, that looks like this:
How do I change the July value? I need this to be 100% automated and change it to January. I made a lot of accounts on various websites and need to change them all back to the same birthdate. Yes, I'm aware I'll have to find the ID of it, etc.
View the HTML of the website and identify the id and values of the dropdownlist, for example:
<select id="bdayMonthId" size="1" name="bdayMonth">
<option value="">Month</>
<option value="Jan">January</>
<option value="Feb">February</>
<option value="Mar">March</>
</select>
To pre-select the dropdownlist value in the WebBrowser control use this Winform code:
webBrowser1.Document.GetElementById("bdayMonthId").SetAttribute("value", "Feb");
I'm a bit confused about your question. If you just want to alter an element in the HTML, Jeremy's answer is the best, and simplest, way to go. If you wanted to call the document's javascript, this should work:
Let's assume that the webbrowser's document contains the following HTML:
<html>
<head>
<title>Invoke Test</title>
</head>
<body>
<div id="testdiv">Waiting...</div>
<script>
function changeDate(date) {
var x=document.getElementById("testdiv");
x.innerHTML = date;
}
</script>
</body>
</html>
To invoke the webbrowser document's javascript method, you can use something like this:
private void button1_Click(object sender, EventArgs e)
{
object o = webBrowser1.Document.InvokeScript("changeDate('june')");
}
No System.Web, ASP, ScriptManagers or Interop are needed. All the tools you need to control webbrowser and document objects and events come with the webbrowser control.

Call JQuery from C# PageLoad

Is there any way how to call JQuery function from PageLoad C# file?
Basically I have some selects, some inputs which are not generated by C# code but are defined in .aspx file manually. When I send form get query to another page I would like to set same variables that are defined in querystring. I know how to do that when I use runat="Server" but I want pure JQuery solutions without having runat="server" objects.
Example:
Select input:
<form method="get" action="/list/search">
<select id="txtSearchFullTyp" name="typ">
<option value="all">Sell, Rent</option>
<option value="1">Sell</option>
<option value="2">Rent</option>
<select>
</form>
Now after sending querystring to another page I am analyzing querystring and running function with defined parameter.
I want to be able to set form to querystring defined as "typ". I dont know how to do it from C# when I dont have runat="Server" option.
Is there a way to do this?
Thanks.
Here's a code for it. It's quite hacky but nevertheless it answers your question:
<form method="get" action="/list/search">
<select id="txtSearchFullTyp" name="typ">
<option value="all">Sell, Rent</option>
<option value="1">Sell</option>
<option value="2">Rent</option>
<select>
</form>
<asp:Literal runat="server" ID="litJqueryCode"/>
and from the code behind you can do the following
litJqueryCode.Text = "<script type='text/javascript'>$(function(){ $('#txtSearchFullTyp').val('"+ Request.QueryString["dropdownValue"] +"') })</script>";

Categories