CODE:
package com.funcybernation.yndextesting;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
}
Output =====
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlBody[<body bgcolor="#fff">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="Cheese!" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
Page title is: Cheese! - Search Google
That log information might be coming from HtmlUnit. If so, you should be able to configure HtmlUnit to reduce the logging.
Related
I need to upload product details and product images into ASP.NET core web API. I implement HTML files as below
<form [formGroup]="myForm" (ngSubmit)="submit()">
<mat-card>
<h2 class="fw-bold text-center">Product Management</h2>
<label class="fw-bold" for="">Product Name</label>
<input type="text" name="name" id="name" formControlName="ProductName" class="form-control">
<input type="text" name="description" id="description" placeholder="Product Description" class="form-control mt-3 mb-2" formControlName="ProductDescription">
<input type="text" name="price" id="price" placeholder="Product Price" class="form-control mt-3 mb-2" formControlName="price">
<input type="text" name="created" id="created" placeholder="Product created" class="form-control mt-3 mb-2" formControlName="created">
<input type="text" name="cat" id="cat" placeholder="Product created" class="form-control mt-3 mb-2" formControlName="ProductCatID">
<input type="file" name="Image" id="Image" class="form-control mt-3 mb-2" (change)="onFileChange($event)" formControlName="ImageUrl">
<img [src]="imageSrc" *ngIf="imageSrc" style="height: 300px; width:500px">
<button type="submit" class="btn btn-primary btn-block mt-3">Submit</button>
</mat-card>
</form>
This is TS file i have implement for the submit data
submit(){
console.log(this.myForm.value);
this.http.post('https://localhost:5001/api/Products', this.myForm.value)
.subscribe(res => {
console.log(res);
alert('Uploaded Successfully.');
})
}
I have implemented Controller As bellow. this controller not read
[HttpPost]
public async Task<ActionResult<ProductDto>> CreateProductAsync(CreateProductDto pro, IFormFile Image)
{
try
{
if (Image == null || Image.Length == 0)
{
return Content("File not selected");
}
var path = Path.Combine(_environment.WebRootPath, "wwwroot//images", Image.FileName);
//Saving the image in that folder
using (FileStream stream = new FileStream(path, FileMode.Create))
{
await Image.CopyToAsync(stream);
stream.Close();
}
pro.ImageUrl = Image.FileName;
var productEntity = _mapper.Map<Product>(pro);
var newProduct = _SqlService.AddProduct(productEntity);
var productForReturn = _mapper.Map<ProductDto>(newProduct);
return CreatedAtRoute("GetProduct", new { id = productForReturn.ProId },
productForReturn);
}
catch(Exception ex)
{
return StatusCode(500, $"Internal server error: {ex}");
}
}
Folder Structure
I need to upload images into the web API folder and as well as I need store the image and product details in SQL Database
When I run the application with ISS after clicking on a class.
The browser starts up with this url. http://localhost:50282/
When I click on Index in my folder Account In Views and
run the application I get this url: http://localhost:50282/Account/Index
Now on both urls I have a register form that links to an action
in my AccountController.
When I submit the form in the second case I get this url:
http://localhost:50282/Account/register and the register method is run in my AccountController Class and works fine.
In the first case I get this url and error:
Url: http://localhost:50282/register
Error: 404
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /register
I want the url to go to the second url http://localhost:50282/Account/register
after clicking register no matter where I clicked before running the application.
Submit form view code:
#{
ViewBag.Title = "Index";
}
<h1>Register Form </h1>
<form action="register" method="post">
<label><i class="" aria-hidden="true"></i> Username </label>
<input type="text" name="Username" placeholder="Enter User Name" required="" />
<br>
<label><i class="" aria-hidden="true"></i> password </label>
<input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" />
<input type="submit" value="Register">
</form>
You need to specify where your form is being submitted to. So change this...
<form action="register" method="post">
<label>
<i class="" aria-hidden="true"></i> Username </label>
<input type="text" name="Username" placeholder="Enter User Name" required="" />
<br>
<label>
<i class="" aria-hidden="true"></i> password </label>
<input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" /> <input type="submit" value="Register">
</form>
To a html helper for your form...
#using(Html.BeginForm("Register", "Account"))
{
<label><i class="" aria-hidden="true"></i> Username </label>
<input type="text" name="Username" placeholder="Enter User Name" required="" />
<br>
<label><i class="" aria-hidden="true"></i> password </label>
<input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" />
<input type="submit" value="Register">
}
Also you need to add antiforgery token for security (#html.Antiforgerytoken()). And decorate Register action with [ValidateAntiforgeryToken] attribute.
See this
I'm new to VB.net and I'm working on a project in Visual Studio 2015. In one of my application's tab I'm trying to INSERT new records into a SQL Table via form post, but once I click the button nothing happens and no new records are added. My current code is below, any help would be great! Thanks:
Contact.aspx
<%# Page Title="Contact" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Contact.aspx.vb" Inherits="Contact" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<head>
<meta charset="utf-8">
</head>
<body>
<br />
<p style="font-size: 15px;">Fill out the form below and click SUBMIT once completed:</p>
<form id="submitData" method="post">
<fieldset>
<p><label for="Term">Term:</label>
<input type="text" name="Term" required />*
</p>
<p><label for="genre">Definition:</label>
<input type="text" name="Definition" required />*
</p>
<p><label for="year">Long Description:</label>
<textarea rows="3" type="text" name="LongDescription" required></textarea>*
</p>
<p><label for="title">Category:</label>
<select name="Category" style="width: 312px; height: 31px; font-family: Arial, Helvetica, sans-serif; font-size: 1.2em;">
<option selected="selected">
<option value="Branding">Branding</option>
<option value="Business">Business</option>
<option value="Business Management">Business Management</option>
<option value="CRM">CRM</option>
<option value="Communications">Communications</option>
<option value="Dental Health">Dental Health</option>
<option value="Education">Education</option>
<option value="General">General</option>
<option value="Governmental">Governmental</option>
<option value="IT">IT</option>
<option value="Marketing">Marketing</option>
<option value="Nutrition">Nutrition</option>
<option value="State Laws">State Laws</option>
<option value="Other">Other...</option>
</select>
</p>
<p><label for="title">Date Loaded:</label>
<input type="text" name="DateLoaded" id="datepicker" required />*
</p>
<p><label for="title">Provided By (your name):</label>
<input type="text" name="ProvidedBy" required />*
</p>
<p><label for="title">Department:</label>
<input type="text" name="Department" required />*
</p>
<p><label for="title">Internal Expert or SME for this topic:</label>
<input type="text" name="SME" />
</p>
<p><asp:Button ID="buttonSubmit" runat="server" OnClick="buttonSubmit_Click" Text="Submit" /></p>
</fieldset>
</form>
<br />
<p>* Please allow between 24 to 48 hours to have your term added to the dictionary.</p>
<br />
<a style="font-size: 16px; font: bold; background-color: black; color:white; text-decoration:none; padding:5px;" href="Default.aspx">Back</a>
</body>
</asp:Content>
Contact.aspx.vb
Partial Class Contact
Inherits Page
Protected Sub buttonSubmit_Click(sender As Object, e As EventArgs)
Dim sqlConnection1 As New Data.SqlClient.SqlConnection("ConnectionString HERE")
Dim cmd As New Data.SqlClient.SqlCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "INSERT INTO table_terms (Term, Definition, LongDescription, Category, DateLoaded, ProvidedBy, Department, SME) VALUES (Term, Definition, LongDescription, Category, DateLoaded, ProvidedBy, Department, SME)"
cmd.Connection = sqlConnection1
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
End Sub
End Class
Any hint would be great. Thanks a lot!
M.C.
Your command text is jacked up. Strings need to be quoted, for instance. Try running the statement in SSMS. If it doesn't work from there it's not going to work from .Net.
Also, what you're doing is a security issue. Use stored procedures and pass values as parameters. Building up a statement to execute in .Net will lead both to worse performance and injection vulnerability.
I have a webpage that has 3 file inputs. There are specific fields on the form that needs to be sent when user uploads the file. I am not able to figure out how do i add custom data to my POST and how do i retrieve it back on the server. This is how my code looks like:
ASPX Page with 3 file inputs and other text boxes/dropdowns:
<form action="FilesUploader.ashx" method="post">
<div id="dvNewAttachment1">
<span>Attachment Type</span>
<select id="ddlAttachmentType1">
<option>T1</option>
<option>T2</option>
<option>T3</option>
</select>
<span>Description</span>
<input id="txtDesc1" />
<select id="ddlApproval1">
<option>Yes</option>
<option>No</option>
</select>
<input id="fileUploader1" type="file" runat="server" />
</div>
<br />
---------------
<div id="dvNewAttachment2">
<span>Attachment Type</span>
<select id="ddlAttachmentType2">
<option>T1</option>
<option>T2</option>
<option>T3</option>
</select>
<span>Description</span>
<input id="txtDesc2" />
<select id="ddlApproval2">
<option>Yes</option>
<option>No</option>
</select>
<input id="fileUploader2" type="file" runat="server" />
</div>
<br />
-------------------------------
<div id="dvNewAttachment3">
<span>Attachment Type</span>
<select id="ddlAttachmentType3">
<option>T1</option>
<option>T2</option>
<option>T3</option>
</select>
<span>Description</span>
<input id="txtDesc3" />
<select id="ddlApproval3">
<option>Yes</option>
<option>No</option>
</select>
<input id="fileUploader3" type="file" runat="server" />
</div>
<input type="submit" />
</form>
This is how my handler looks like:
public void ProcessRequest(HttpContext context)
{
HttpPostedFile myFile = context.Request.Files[0];
int nFileLen = myFile.ContentLength;
byte[] buffer = new byte[nFileLen];
using (BinaryReader br = new BinaryReader(myFile.InputStream))
{
br.Read(buffer, 0, buffer.Length);
}
}
As you can see i have 3 uploads and each has Attachment Type and description associated with it which i need to retrieve for each input file in my handler.
Right now i am just processing file from the first input but later i am going to loop thru the inputs and process them.
If your handler do not receive files that you place in file uploaders then your form needs to have additional attribute like this:
<form action="FilesUploader.ashx" method="post" enctype="multipart/form-data">
In order to get values from dropdowns on server you need to provide a name attribute to them:
<select id="ddlApproval1" name="ddlApproval1">
<option>Yes</option>
<option>No</option>
</select>
I am using wordpress 3.3.1 with twentyten theme,
i have created a plugin to create a custom form,
i have successfully installed this in wordpress,
my plugin file code is as follows
<?php
function guest_event_form()
{
if(isset($_POST['submit']) and $_POST['action']=='new registration')
{
global $wpdb;
$wpdb->query("Insert Query...");
}
else
{
?>
<form method="POST" action="" name="guest_registration" enctype="multipart/form-data">
<input type="text" id="name" name="name" value="">
<input type="submit" name="submit" value="Register Me Now"/>
<input type="hidden" name="action" value="new registration" />
</form>
<?php
}
}
add_shortcode( 'guest_event_form', 'guest_event_form' );
?>
whenever i am submitting this form, i returns to same page with search results,
so i guess the problem whenever i submit this form, wordpress takes this submit action as a search action, and it starts search
how do i overcome this problem??
The Problem is because of following form element's id
<input type="text" id="name" name="name" value="">
name is one of the wordpress internal variable
Change it like this:
<input type="text" id="customername" name="customername" value="">