get attributeby is not working - java

String a=driver.findElement(By.id(locator))).getAttribute("value");
System.out.println("the value is "+ a);
Log.info(" Required object is present in the applicaiton"+ object);
<p locale="" isnumeric="false" basetype="String" class="outputText twControl twOutputText" id="InputText12">**MANTPLAP29102014100955627**</p>
I need to get the MANTPLAP29102014100955627 value from the above HTML code.
Please help..

Use
driver.findElement(By.id(locator))).getText();
Edit
There is a difference between getAttribute and getText methods
Consider an html tag like this
<input type = "text" id = "id" name = "bird">Peagon</input>
Now using
driver.findElement(By.id("id"))).getAttribute("name") will return value as bird (which is the value of attribute 'name')
and using
driver.findElement(By.id(locator))).getText(); will return a value as Peagon (Text between the starting and closing <input> tags)

Related

HtmlUnit asNormalizedText() returns empty string

I have this code:
HtmlPage rowPage = ...
String address1 = ((HtmlDivision)rowPage.getFirstByXPath("//div[contains(#class, 'client_address1')]")).asXml();
System.out.println("address1 = " + address1);
String address1_2 = ((HtmlDivision)rowPage.getFirstByXPath("//div[contains(#class, 'client_address1')]")).asNormalizedText();
System.out.println("address1_2 = " + address1_2);
and my output is:
address1 = <div class="client_address1 clientRow">
123 Somewhere ln
</div>
address1_2 =
I expect asNormalizedText() to return 123 Somewhere ln. What circumstances would cause asNormalizedText to return nothing?
A bit more specific XPath would help
//div[contains(#class, 'client_address1')]/text()
What circumstances would cause asNormalizedText to return nothing?
From the javadoc:
Returns a normalized textual representation of this element that represents
what would be visible to the user if this page was shown in a web browser.
Please check the css class - maybe the style hides the text.

SELENIUM JAVA - Verify partial content of a string

I would like to verify the partial value of a string I get from a web page. I give an example the string is "Incident 1946721 Updated" and I would like to insert a check that verifies that the two words Incident are present as prefix and Updated as suffix. how can I do?
the html code where the text is present this is:
<div class="modal-body"><button type="button" class="bootbox-close-button close" data-dismiss="modal" aria-hidden="true" style="margin-top: -10px;">×</button><div class="bootbox-body">Incident 1946721 Updated</div></div>
Use below code :
String value = driver.findElement(By.xpath("//div[#class='bootbox-body']")).getText();
if(value.startsWith("Incident") && value.endsWith("Updated")) {
System.out.println("Test Pass");
}else {
System.out.println("Test Fail");
}
Let me know if you have further query.

Setting id attribute on input field using Wicket MultiFileUploadField

In my panel class I have the following code:
private Fragment fileUploadField(String id, UploadFeedbackPanel feedbackPanel, ComponentFeedbackPanel componentFeedbackPanel) {
String uploadType = isJSEnabled ? "multiple" : "single";
Fragment uploadFragment = new Fragment( "uploadContainer", uploadType, this );
if (isJSEnabled) {
multipleUpload = new MultiFileUploadField( id, new PropertyModel<Collection<FileUpload>>( this, "multiUploads" ), MAX_FILES );
uploadFragment.add( multipleUpload = multipleUpload);
multipleUpload.add( newOnChangeAjaxBehavior( feedbackPanel, componentFeedbackPanel ) );
} else {
uploadFragment.add( singleUpload = new FileUploadField( id ) );
singleUpload.add( newOnChangeAjaxBehavior( feedbackPanel, componentFeedbackPanel ) );
}
return uploadFragment;
}
I want to add a label for this field but I'm unable to get the actual input fields ID. You can see this working for the single upload field because the input field itself is render without any surrounding elements. This however doesn't seem to be exposed when using MultiFileUploadField.
An alternative acceptable answer would be using FileUploadField and a collection of files with the multiple=true attribute. However I am unsure how to limit the number of files to be MAX_FILES only.
<label wicket:for="file"><wicket:msg key="file">File:</wicket:msg></label>
<div wicket:id="uploadContainer" class="col-right">[upload fragment shows here]</div>
<wicket:fragment wicket:id="single">
<input wicket:id="file" type="file"/>
</wicket:fragment>
<wicket:fragment wicket:id="multiple">
<div wicket:id="file" class="mfuex"></div>
</wicket:fragment>
Wicket version 6.15.0.
MultiFileUploadField uses JavaScript to generate the input fields: https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.js#L91
See whether you can plug there somehow. If you find an elegant way we would be glad to include it in the next version of Wicket!
If you use 'multiple' attribute then check:
How do I limit the number of file upload in html?

populate hidden input form fields based on URLparameters

I have a ppc landing page that I want to change the hidden value of a form field based on the URL parameter.
The field
<input id="Campaign_ID" name="Campaign_ID" type="hidden" value="7g012455dv5441vdf">
The URL would be something like mysite.com/?campaignidvalue=7g012455dv5441vdf
There will be other field "values" that are also based on the URL parameter, so it has to tie the "input id" (or name) to that specific value.
EDIT: There was an error in my code.
PHP would be the easiest!
<?php
if(!empty($_GET['campaignidvalue']))
{
echo '<input id="Campaign_ID" name="Campaign_ID" type="hidden" value="'. $_GET['campaignidvalue'].'"/>';
}
?>
This block will check to see if the value is passed in a GET parameter, and then echo it out into a hidden form field.
The first part:
if(!empty($_GET['campaignidvalue']))
Is checking to see if a parameter named Campaign_ID is being passed.
So your URL of:
mysite.com/?campaignidvalue=7g012455dv5441vdf
Would allow the code to continue, as it is not empty.
This section is the part that actually displays it on the page.
echo '<input id="Campaign_ID" name="Campaign_ID" type="hidden" value="'. $_GET['campaignidvalue'].'"/>';
If you view the source of the HTML page, you would see
By using this function:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null) {
return "";
} else {
return decodeURIComponent(results[1]);
}
}
You'll be able to get any URL parameter by name, like so:
var cid = getParameterByName(campaignidvalue);
Then, once you have the value you can use jQuery to set it, like so:
$(function() {
$("#Campaign_ID").val(cid);
});

check parameter is valid or not in jsp

I just want to check that the parameter passed to the jsp are null or not using httpcontext so how i can able to do that
case_yr = Integer.parseInt(request.getParameter("case_yr"));
sub_type_int =Integer.parseInt(request.getParameter("sub_type_int"));
String case_num = request.getParameter("case_num");
String lang_mode = request.getParameter("lang_mode");
String mobile_no = request.getParameter("caller_id");
String dialled_id = request.getParameter("dialled_id");
i want to above values are valid or not I am submitting value from xml page to jsp page and getting value in jsp using request.getparameter()
so pleaase tell how to check values are null or not and there is no sevlet only vxml page and jsp page
Use JSTL
<c:if test="${not empty case_num}">
case_num is NOT empty or null.
</c:if>
if you wanna check if the request attributes are null,just check if they are null this way
String case_num = request.getParameter("case_num");
if(case_num!=null){
//do your logic
}
else {
System.out.println("case_num is null");
}
int number ;
String num = request.getParameter(num);
if(num!=null){
number = Integer.parseInt(num);
}
else {
System.out.println("case_num is null");
}

Categories