I want to select input fields from the following form in selenium. I have tried many methods but found no help and my test always fails.
Here is the html form:
`<form class="login" action="/Index/trylogin/?ru=L015RmlueWE=" novalidate>
<fieldset>
<label>
<input type="text" maxlength="14" tabindex="1" value="" placeholder="Pseudonym">
</label>
<label>
<input type="password" maxlength="20" tabindex="2" data-ch="8H7vtP9f9tns3TGMJ6F995kTyLSmwFsdDlIyBLhBBsrrW1ZHIZiec4cPqF7C5sp5" data-ch2="1782447a8c3759d4407ed522b831806e8cfde5dc" placeholder="Kennwort">
</label>
<input type="submit" value="Anmelden" class="button button-style1">
<div class="login-options">
<label>
<input type="checkbox" value="1" name="stayon">Automatisch anmelden (auf diesem Gerät)
</label>
</div>
</fieldset>
</form>`
Her is my test method
[TestMethod]
public void Login_FinYa_System()
{
IWebElement login = _driver.FindElement(By.XPath("//form[1]"));
IWebElement pass = _driver.FindElement(By.XPath("//form[2]"));
login.Click();
login.SendKeys("helo");
pass.Click();
pass.SendKeys("123");
pass.SendKeys(Keys.Enter);
Assert.AreEqual("hello", "hello");
}
You are using incorrect xpath for login and password field.
Try this.
WebElement login = driver.findElement(By.xpath("//input[#type='text']"));
WebElement password = driver.findElement(By.xpath("//input[#type='password']"));
login.click();
login.sendKeys("hello");
pass.click();
pass.sendKeys("123");
pass.sendKeys(Keys.Enter);
Assert.assertEquals("hello", "hello");
Related
I am a new .NET programmer. I have the following input:
<input asp-for="FirstName"
id="FirstName"
class="form-control"
value="#Context.Request.Query["FirstName"]"
type="text"
readonly="readonly" />
If the textbox has data, I want the textbox to be readonly, if there is no data in the textbox, then the user can input data. Can anybody give advice?
If the textbox has data, I want the textbox to be readonly, if there
is no data in the textbox, then the user can input data. Can anybody
give advice?
According to your code and description, it seems that you want to set the textbox to be read-only based on the FirstName query string parameter. I suggest you could try to use the following code (use Razor syntax):
#model MVCDemo.Models.Customer
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
#{
var value = Context.Request.Query["FirstName"].ToString();
if (string.IsNullOrEmpty(value))
{
<input asp-for="FirstName" id="FirstName" class="form-control" value="#value" type="text" />
}
else
{
<input asp-for="FirstName" id="FirstName" class="form-control" value="#value" type="text" readonly />
}
}
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
The screenshot as below:
Besides, if you want to make the textbox read-only after entering some value, you could use JQuery to add or remove the readonly attribute on the textbox.
You can do something like this
#{
var firstname= Context.Request.Query["FirstName"];
}
<input asp-for="FirstName" id="FirstName" class="form-control"
value="#Context.Request.Query["FirstName"]" type="text"
readonly="#(Context.Request.Query["FirstName"]=="" ? "False" : "True")">
Hi I'm trying to make a Razor page with a simple form that add the new element into a list, but the OnPost() method is never calling. I have a breakpoint on the first line of the method and it never hits, but in the OnGet() it works.
This is my OnPost() method:
public void OnPost()
{
newDocument = Int32.Parse(Request.Form[nameof(newDocument)]);
newAge = Int32.Parse(Request.Form[nameof(newAge)]);
newTriage = (Triage)Int32.Parse(Request.Form[nameof(newTriage)]);
newName = Request.Form[nameof(newName)];
newGender = Request.Form[nameof(newGender)];
newSymptoms = Request.Form[nameof(newSymptoms)];
var newPatient = new Patient
{
Document = newDocument,
Name = newName,
Age = newAge,
Gender = newGender,
Triage = newTriage,
Symptoms = newSymptoms
};
patients.Add(newPatient);
OrderPatients();
}
And the razor page have this form:
<form>
<div class="form-group">
<label for="patientDoc">Documento</label>
<input asp-for="newDocument" type="number" class="form-control" id="patientDoc" placeholder="ingrese documento">
</div>
<div class="form-group">
<label for="patientName">Nombre</label>
<input asp-for="newName" type="text" class="form-control" id="patientName" placeholder="Nombre">
</div>
<div class="form-group">
<label for="patientAge">Edad</label>
<input asp-for="newAge" type="number" class="form-control" id="patientAge" placeholder="Edad">
</div>
<div class="form-group">
<label for="patientGender">Género</label>
<input asp-for="newGender" type="text" class="form-control" id="patientGender" placeholder="Género">
</div>
<div class="form-group">
<label for="patientTri">Prioridad</label>
<select asp-for="newTriage" class="form-control" id="patientTri">
#foreach (Triage tri in Enum.GetValues(typeof(Triage)))
{
<option>#tri</option>
}
</select>
</div>
<div class="form-group">
<label for="patientSymp">Sintomas</label>
<input asp-for="newSymptoms" type="text" class="form-control" id="patientSymp" placeholder="Sintomas">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I have read that you have to put this line #addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers at the beginning of the page, but I did it and still the OnPost doesn't hit.
Anyone knows what is wrong?
The default method type for form is GET, so it hits your OnGet handler because you didn't specify the method. Change <form> to <form method="post"> and it should work.
Forget it. I just had to add the method="form" at the beginning of the form like this:
<form method="post">
. . .
</form>
Gonna leave this around here for another newbie like me who might need it.
I am currently struggling with Selenium to select an element by its class name.
The Website I'm using is:
http://demo.guru99.com/test/login.html
<div class="col-xs-12 col-sm-6">
<form action="success.html" method="post" id="login_form" class="box">
<h3 class="page-subheading">Already registered?</h3>
<div class="form_content clearfix">
<div class="form-group">
<label for="email">Email address</label>
<input class="is_required validate account_input form-control" data-validate="isEmail" type="text" id="email" name="email" value="">
</div>
<div class="form-group">
<label for="passwd">Password</label>
<span><input class="is_required validate account_input form-control" type="password" data-validate="isPasswd" id="passwd" name="passwd" value=""></span>
</div>
<p class="lost_password form-group">Forgot your password?</p>
<p class="submit">
<input type="hidden" class="hidden" name="back" value="my-account"> <button type="submit" id="SubmitLogin" name="SubmitLogin" class="button btn btn-default button-medium">
<span>
<i class="icon-lock left"></i>
Sign in
</span>
</button>
</p>
</div>
</form>
</div>
I figured out how to select the element by id but whenever I try to select it by classname I get an element not found exception. I don't really understand how this works as I tried multiple websites without success. In this example the element has multiple classes but on some websites this method even fails when there is only one class. On this website selecting it by only one class works but I have read that this isn't a good method as you could be selecting multiple elements by mistake.
IWebElement search = driver.FindElement(By.ClassName("account_input"));
Here is my code:
driver = new ChromeDriver();
driver.Url = "http://demo.guru99.com/test/login.html";
driver.Navigate();
IWebElement search = driver.FindElement(By.ClassName("is_required.validate.account_input.form-control"));
search.SendKeys("test");
In this example selecting element by Id using the By.Id("first_name") method doesn't work either:
<span>
<label class="first_name_label form_label_text"></label>
<div class="clear"></div>
<div class="form-item icon">
<input value="" data-error="An error has occured." id="first_name" type="text" name="first_name" class="form_item is_required" placeholder="First Name">
<i style="color:rgba(70,75,106,0.5)" class="vs icon-user"></i> </div>
<div class="form-item icon">
<input value="" data-error="An error has occured." id="last_name" type="text" name="last_name" class="form_item is_required" placeholder="Last Name">
<i style="color:rgba(70,75,106,0.5)" class="vs icon-user"></i> </div>
<div class="clear"></div>
</span>
What am I doing wrong? Help would be greatly appreciated
You can use the class name to find the element as well, But what is the problem is that same class can be used to define multiple element in the program , So when you find element by class it will not assure you that it will find the element which you are hoping for.
Selenium will find element of the first in the result.
Better to with xpath like tag along with the class.
Here is the code
xpath = " //input[#name='passwd'] ";
# -> can be used to get any parameter inside the tag and use it as per your requirement.
This will help you.
Contact for more information.
Corrected Code: Xpath of your class name was incorrect.
driver = new ChromeDriver();
driver.Url = "http://demo.guru99.com/test/login.html";
driver.Navigate();
IWebElement search = driver.FindElement(By.ClassName("is_required validate account_input form-control"));
search.SendKeys("test");
Another way to access email field is:
IWebElement search = driver.FindElement(By.Xpath("//*[#id='email']"));
Hope this helps.
If you will observe the class name of the element, it is the compound class name(having space in between). And compound class names are not permitted in selenium. In this case, you can use another locator like CSS(with class name) or XPath.
Check the following code snippet:
WebDriverManager.chromedriver().version("2.40").setup();
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/test/login.html");
WebElementuserName=driver.findElement(By.cssSelector(".is_required.validate.account_input.form-control"));
userName.sendKeys("Username");
WebElement password = driver.findElement(By.xpath("(//[#class='is_requiredvalidateaccount_input form-control'])[2]"));
password.sendKeys("Password");
I have a site with a "Contact Us" page and I want my users to be able to use that form to send me an email. My email, of course, does not show, but I have the user place their first name last name, email address, and a message in a bootstrap 4.xxx (the latest one) form and hit send. However, when I hit send the data does not get populated to the ActionResult method in my HomeController.
I also noticed an error with my javascript function on-click submit, the error is it is null. I am in the process of using the Route-Config to help catch the data, but I am not sure which is the right path at this point. I do not want to use the Html helpers (just the HtmlBeginForm).
I have had many iterations and so this not exactly the best, but it is enough to see my issues.
HomeController:
[HttpPost]
public async Task<ActionResult> Email(FormCollection form)`enter code here`
{
var firstName = form["firstName"];
var lastName = form["lastNname"];
var email = form["senderEmail"];
var message = form["emailMessage"];
var x = await ContactForm(firstName, lastName, email, message);
return RedirectToAction("Send");
}
private async Task<ActionResult> ContactForm(string firstName, string lastName, string email, string message)
{
try
{
MailMessage messages = new MailMessage();
messages.To.Add("marketing#gmail.com");
messages.From = new MailAddress(email);
messages.Subject = "Contact Page Email";
messages.Body = message;
messages.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "marketing#gmail.com",
Password = "*****"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail-outlook.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(messages);
}
}
catch (Exception ex)
...
HTML:
#using (Html.BeginForm("Email", "Home", FormMethod.Post))
{
<form name="sendMessage" id="contactForm">
<div class="form-row">
<div class="col">
<label for="firstName">First Name:</label>
<input type="text" class="form-control" placeholder="First Name" id="firstName" name="firstName">
<div class="invalid-feedback">Enter a first name.</div>
</div>
<div class="col">
<label for="lastName">Last Name:</label>
<input type="text" class="form-control" placeholder="Last Name" id="lastName" name="lastName">
<div class="invalid-feedback">Enter a last name.</div>
</div>
</div>
<div class="form-group">
<label for="inputEmail">Email Address:</label>
<input type="email" class="form-control" id="email" placeholder="username#emailaddress.com" name="senderEmail">
</div>
<div class="form-group">
<label for="emailMessage">Message:</label>
<textarea class="form-control" id="message" rows="5" name="emailMessage"></textarea>
<div class="invalid-feedback">Enter a message.</div>
</div>
<button type="submit" class="btn btn-primary btn-lg " onclick="myFunction()">Send</button>
</form>
Email with first name, last name, sender's email, message in my inbox.
You are using FormCollection in your Email method which requires the cshtml to have a name parameter for the fields which is missing in your code.
Also, you don't really require javascript function to submit the form unless you want to intervene and do some validations. Hence, please try removing that as well.
Please try to change your cshtml to below:
#using (Html.BeginForm("Email", "Home", FormMethod.Post))
{
<div class="form-row">
<div class="col">
<label for="firstName">First Name:</label>
<input type="text" class="form-control" placeholder="First Name" id="firstName" name="firstName">
<div class="invalid-feedback">Enter a first name.</div>
</div>
<div class="col">
<label for="lastName">Last Name:</label>
<input type="text" class="form-control" placeholder="Last Name" id="lastName" name="lname">
<div class="invalid-feedback">Enter a last name.</div>
</div>
</div>
<div class="form-group">
<label for="inputEmail">Email Address:</label>
<input type="email" class="form-control" id="email" placeholder="username#emailaddress.com" name="senderEmail">
</div>
<div class="form-group">
<label for="emailMessage">Message:</label>
<textarea class="form-control" id="message" rows="5" name="smessage"></textarea>
<div class="invalid-feedback">Enter a message.</div>
</div>
<button type="submit" class="btn btn-primary btn-lg">Send</button>
}
I am trying to auto-fill a form on a webpage using .NET's web browser int he C# language. I need to fire the submit button but it doesn't have a name or ID, all it has is a type and value.
The type is equal to "submit" and the value is equal to "Sign In"
Heres how I did it for Sign Up:
webBrowser1.Document.GetElementById("email").InnerText = email;
webBrowser1.Document.GetElementById("password").InnerText = password;
webBrowser1.Document.GetElementsByTagName("input")["register"].InvokeMember("click");
But this time, the submit button doesn't have a name, can someone guide me in the right direction to getting an element by its type or even better, value?
HTML:
<form accept-charset="utf-8" method="post">
<div class="field">
<label for="email">Email or Username</label> <span class="required">*</span> <input class="email" id="email" name="email" size="32" type="text">
</div>
<div class="field">
<label for="password">Password</label> <span class="required">*</span> <input id="password" name="password" size="16" type="password">
<p class="tip">I don't know my password</p>
</div>
<div class="field">
<input id="persistent_hidden" name="persistent" type="hidden" value="f"> <input checked="checked" id="persistent" name="persistent" type="checkbox" value="t"> <label for="persistent">Keep me logged in</label>
</div>
<div class="submit">
<input id="redirect_path" name="redirect_path" type="hidden"> <input type="submit" value="Sign In"> or <a class="cancel" href="/">Cancel</a>
</div>
</form>
try :
foreach(HtmlElement elem in webBrowser.Document.getElementByTagName("input")) {
if (elem.GetAttribute("value") == "Sign In") {
elem.InvokeMember("Click");
}
}