I've been trying to get the value from a hidden input and innertext from a span on a website (note that I don't have access to edit the HTML code).
Code from website:
<input type="hidden" name="hidv" value="1582912961">
<span class="valo">0.888</span>
I could use a code similar to this but don't know the correct use of it:
label5.Text = webBrowser2.Document.GetElementById("hidv").InnerText;
label6.Text = webBrowser2.Document.GetElementById("valo").InnerText;
First I'd put an ID on the input tag
<input id="myId" type="hidden" name="hidv" value="1582912961">
Then you could use JS, jQuery, etc, to select the value.
$('#myId').val();
$('input[name=hidv]').val()
also just a note Document.GetElementById requires an Id. Otherwise, you could've done Document.GetElementsByClassName("valo") if you wanted to use the class name instead of an Id attribute.
Related
Button type is Input.
<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit btn solid">
I want to click this Button using code, try to click on Button By ClassName.
My code is as per below but not get any success.
string url1 = "myUrl";
IWebDriver driver = new ChromeDriver(Application.StartupPath);
driver.Navigate().GoToUrl(url1);
driver.FindElement(By.ClassName("wpcf7-form-control .wpcf7-submit btn solid")).Click();
This throws the following error :
Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead. and not use single class because there is a other class with same name.
driver.FindElement(By.XPath("//input[#type='submit']")).Click();
I would try to avoid using XPath, Instead I would add an Id to your input.
The reason being XPath is more likely to be changed over time than Id.
Also if you add another element with the same HTML, selenium will not be able to differentiate between them and will always select the first element.
Using Id also makes your code more readable.
You can try this:
<input type="submit" value="Send" Id="YourButtonId" class="wpcf7-form-control wpcf7-submit btn solid">
And
driver.FindElement(By.Id("YourButtonId")).Click();
I am working on a C#.NET web application that use JQuery mobile for its view.
in a view I have a form that contains the following input field in which the user can put an URL:
<label for="nome">URL*:</label>
<input type="text" data-clear-btn="true" name="oval.url" id="url" data-mini="true" data-inline="true" required="required" value="#Model.oval.URL" />
My problem is that, using the previous code snippet, the user can insert any string but I want that it is considered as a valid value only a real URL (something like www.thisisanurl.com and not something this is not an url)
Can I do this check using JQuery? Or what can I do to solve this issue?
Tnx
Try adding this validation:
var urlregex =new RegExp("^(http:\/\/www.|https:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
if (urlregex.test(textval)){
...
}
This could be useful.
Since it is jquery mobile I'll assume it is being viewed on a mobile device. Most mobile browsers support HTML5 input types. http://www.w3.org/TR/html-markup/input.url.html
<input type="url" ... />
I have been facing an issue of putting form validation on html controls any one guide me how to resolve this issue?
My HTML Field In something.cshtml
<input type="text" id="AccidentDate" name="AccidentDate" />
#Html.ValidationMessage("AccidentDate")
Controller code:
[HttpPost]
[AllowAnonymous]
public ActionResult Index(Models.c objC)
{
if (String.IsNullOrEmpty(objC.AccidentDate))
{
ModelState.AddModelError("AccidentDate", "*");
}
return View(objClaimant);
}
Please note validationMessage does raise error in the form of * in-front of that input field but my requirement is to highlight that input field as red.
MOST IMPORTANT I KNOW IF I USE:
#Html.TextBox("AccidentDate", Model.AccidentDate); it works perfectly but using this is not my requirement.
finally, i am looking to make html input field red through any other way, if validation message is raised.
any help would be appreciated.
Ok, so rather than just commenting I figured I'd try and help. So I'm presuming you want to use the unobtrusive validation engine
So if you do
#Html.TextBox("AccidentDate", Model.AccidentDate);
and view source you'll see something like:
<input data-val="true" data-val-required="The AccidentDate field is required."
id="AccidentDate" name="AccidentDate" type="text" value="">
So the important thing to notice are the data-val attributes. These basically tell the unobtrusive engine, "this field needs to be validated and this is how and what to display if it fails"
So to do this without #Html.TextBox which does all this for you, you'll need to create these attributes yourself in your HTML.
Quick an dirty solution, put your #HTML.TextBox in, run your code cut&paste the HTML. May not be what you want?
Edit
you want the required validation, so the above data-val="true" data-val-required="The AccidentDate field is required." attributes are what you need, obviously substituting your own error message
so:
<input type="text" id="AccidentDate" name="AccidentDate"
data-val="true" data-val-required="The AccidentDate field is required." />
#Html.ValidationMessage("AccidentDate")
Css Class
The Css class should be being applied. The relevant piece of code is:
function onError(error, inputElement) { // 'this' is the form element
var container = $(this).find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"),
replace = $.parseJSON(container.attr("data-valmsg-replace")) !== false;
container.removeClass("field-validation-valid").addClass("field-validation-error");
error.data("unobtrusiveContainer", container);
if (replace) {
container.empty();
error.removeClass("input-validation-error").appendTo(container);
}
else {
error.hide();
}
}
inside the jquery.validate.unobtrusive.js. confirm your referencing this js file and see if the above code is hit?
I am trying to get the value of a hidden input in code behind with the following code. I am trying to cast it but it cannot find it , any help ?
((HtmlControl)FindControl("contentId"))
I declare it in aspx with the following code:
<input id="contentId" type="hidden" />
I dont want to runat server because i have my own reasons
To access a HTML control at server side (in your C# code), you need to first add the runat="server" attribute. So, your markup should look like
<input type="hidden" id="contentId" runat="server"/>
Now, in the code behind you can use the control by its id contentId itself if the code behind got generated properly.
Please let us know why you are forced to use the FindControl in the first place as it can be accessed by using the id directly.
Update
As per the comment below, the user for some reason is not interested in making this input a server side control. Then the only possibility by which you can read the values at server side is as below. But this is not advised as any changes to the name goes unnoticed and breaks at runtime.
<input type="hidden" id="contentId" name="contentName" runat="server"/>
In Code
this.Request.Forms["contentName"] would return the hidden value.
Try to search it on the page this way
HiddenField hf = (HiddenField)page.FindControl("contentId");
To get the value:
HiddenField h = (HiddenField)Gridview.FindControl("HiddenFieldName");
Then with that you can put it into a string, if you wish to.
Use this code:
string s=((HiddenField)Panel1.FindControl("contentId")).Value;
Here panel is the container control. This may be a grid or anything else or even a master page. But if you are using FindControl, i think the control may be inside some container.
HtmlInputHidden hf = Page.FindControl("contentId") as HtmlInputHidden;
string hfValue = hf.Value;
I am currently trying to validate my ASP.NET form. I need to be to sure that the user has entered a password of at least 5 characters.
I have done a check to make sure that something is valid using the following code:
else if (Request.Form["txtPassword"] == "")
{}
I am then checking that the characters is not less than 5 by using the following code:
if (Request.Form["txtPassword"].Length < 5)
{}
However, when I run the form and submit it, instead of it displaying the error to the user about the password length, I keep on getting an error from Visual Studio. Before I have tried to submit the form it displays:
Object reference not set to an instance of an object.
This error is only displayed when I am checking the length not if I am checking the String is empty.
Thanks for any help you can provide.
You have a null reference exception. Request.Form["txtPassword"] returns null. This is essentiall what is happening:
string myString = null;
// This is valid comparison because myString is a string object.
// However, the string is not *empty*, it is null, thus it has *no* value.
if(myString == "")
{
DoSomething();
}
// This is not acceptable, as null.Length has no meaning whatsoever.
// The expression compiles because the variable is of type string,
// However, the string is null, so the "Length" property cannot be called.
if(myString.Length < 5)
{
DoSomethingElse();
}
You cannot access the length of a null string. Try using this instead:
var myString = Request.Form["txtPassword"];
if(!String.IsNullOrEmpty(myString) && myString.Length < 5)
{
DoSomething();
}
The next question, however, is WHY is it null? Perhaps you have inaccurately named the associated form input to something other than "txtPassword", or perhaps the data is not being sent via POST.
This probably means that Request.Form["txtPassword"] is null. I would first check that it exists.
Youy receive that error because you are calling the Length property on a null object, in this case Request.Form["txtPassword"] is null, Length cannot be called.
You might want to make sure your textbox has ID "txtPassword" remember that asp.net before .net 4 generates client IDs like "ctl00_txtPassword" and that becomes the Form field and you might need to enter Request.Form["ctl00_txtPassword"].
I had a similar problem. I was trying to post from one project to another. Because of requirements I was stuck using a regular HTML form. The values were always null. I beat my head against the wall with this one until I stumbled on the solution.
1st: NO runat="server" tag in your form header
bad: <form id="form1" runat="server" action="http://yoursite.com/yourpage.aspx" method="post" >
good: <form id="form1" action="http://yoursite.com/yourpage.aspx" method="post" >
2nd:
.Net's html input box is
.Net HTML Tag: <input id="myinput" type="text" />
The closing tag /> is the problem. If you remove the / it will work.
bad: <input id="myinput" type="text" />
good: <input name="myinput" type="text" >
3rd: be sure to use the "name" attribute instead of "id" attribute
bad: <input id="myinput" type="text" />
good: <input name="myinput" type="text" >
If the form tag is placed in Master Page eg:Site.Master, and the form you are submitting contains field
< asp:TextBox ID="amount" name="amount" runat="server" />
and is actually is inside the ContentPlaceHolder (ID="MainContent"), then if you use Request.Form["amount"] it does not work and always shows null value in it...because, when the page is rendered, the id actually becomes "ctl00$MainContent$amount". Hence, Request.Form["ctl00$MainContent$amount"] will work.