Select/click on checkbox by labelin Selenium - java

I have the following html code and would like to select checkbox by label:
<input type="checkbox" onclick="searchResult(this,'8')" id="catalog-8"/>
<label for="catalog-8">
my-assortment </label>
in the above example, by the value "my-assortment"
I tried this: //label[containts('my-assortment')] but it does not work.
More exactly, I want to write something like this:
//input[#type='checkbox'] which has "id" of the value of "for"
in label[contains(., 'my-assortment')]
Does anybody have any idea?

//label[containts('my-assortment')]
First of all, this is not containts - it should be contains (watch the extra t). And, you are not using contains() correctly. It should be:
//label[contains(., 'my-assortment')]
Also, if you want to click the input element by label, preceding-sibling would help here:
//label[contains(., 'my-assortment')]/preceding-sibling::input[#type='checkbox']

Related

How to find placeholder value in Selenium

I need to verify whether placeholder/helper text is displayed for a field.
In this example it is Min. $50.00
I don't see any placeholder/helper text attribute defined inside the div tags.
My field looks like . Displayed amount (in this example $50.00) is dynamic.
Code for the above field looks like
<div class="unit" style="padding-left:32px;">
<input id="Amount" type="text" class="inputAlign optionalHintText" size="18" maxlength="30"/>
<div id="sharesValueForPercent" class="TextMd"></div>
</div>
I tried different ways to get the text (Min$50.00). Can someone help me on this. Appreciate your response.
To extract the placeholder text i.e. $50.00 you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(driver.findElement(By.cssSelector("input.inputAlign.optionalHintText#Amount")).getAttribute("value"));
Using xpath:
System.out.println(driver.findElement(By.xpath("//input[#class='inputAlign optionalHintText' and #id='Amount']")).getAttribute("value"));

Selenium WebDriver findElement(By.xpath()) get value of this element

Part of it look like this:
<input type="hidden" id="recaptcha-token" value="Need This Value">
I'm running Selenium like this:
driver.findElement(By.xpath("[#id=\"recaptcha-token\"]"));
How I can get "Need This Value" from running this code ?
For your specific case, you can first get the element like so:
driver.findElement(By.xpath("//*[#id='recaptcha-token']")); // Any element with that id
driver.findElement(By.xpath("//input[#id='recaptcha-token']")); // More specific to your tag
And then you get the attribute you want using getAttribute(String attrName).
A one-liner would then be:
driver.findElement(By.xpath("//input[#id='recaptcha-token']")).getAttribute("value");
If you only want to look for an element with that id, you could simplify that call by using By.id() instead of By.xpath():
driver.findElement(By.id("recaptcha-token"));

java - How to make Struts radio tag create a vertical list "The OGNL way"

This post has a reference to SO question"How to make Struts radio tag create a vertical list of radio buttons"
only difference is that I don't want to use an iterator.
I just want to do like this.
radio button
* abc
* xyz
I am using struts tag code like this:
<s:radio id="testing"
list="#{'val1':'abc','val2':'xyz'}" />
Please note that I don't want to use a list like this to iterate through the list elements. I'd like to enter those values manually as above.. More or less i just want a tweak like "put a <br> element here and there" to just make the next radio button xyz come to the next line and not be attached to abc in the same line. Hope this explanation makes more clear the solution I'm looking at.
<s:iterator value="aList">
<s:radio key="selectedId" list="{aObject}" listKey="id" listValue="name"/><br/>
</s:iterator>
I didn't know that ognl created map can be used to do this...
The Solution:
<s:iterator value="#{'val1':'abc','val2':'xyz'}" var="some">
<s:radio key="selectedId" list="#some" listKey="key" listValue="value"/><br/>
</s:iterator >

How to set color to some portion of text in radio.setLabel("Phone Number"+cont.getPhone);

I am using Zk framework for UI and from Controller side i need to set the value for the radio.Have a look on below code
radio.setLabel("Phone Number#"+"<span style="+"\" foreground-color:blue \""+">"+cont.getPhone().toString() +"</span>"+"Email Id"+cont.getEmail());
but it is not replacing the color,only color of phone number should be replaced
Try below line of code
If you want to do in ZUl page try this
<radio id="radiog" label="Item D" value="itemD" style="color:blue;"/>
otherwise try
radio.setStyle("color:blue;");//Their can be syntax error please modify it according to your requirement.
Take a look at my fiddle. I separate your radio in 3 components: 1 radio and 2 labels, all inside 1 div. This is the only simple way I can think of
Fiddle
Code
<zk>
<window border="normal" title="hello">
<div>
<radio id="radio" value="itemD" />
<label value="Phone: " />
<label style="color:blue" value="123456" />
</div>
</window>
</zk>
I doubt ZK allows use of HTML code inside the radiobutton label.
I suggest breaking it into smaller pieces like #AlexGreg answer shows, you can even consider using spans or try a CSS approach like the one suggested in https://stackoverflow.com/a/4622818/1385048.
You can override the client widget's implementation domLabel_() function as follows:
<radio xmlns:w="client">
<attribute name="onCreate"><![CDATA[
self.setLabel("<span style='color:blue'>test</span>");
]]></attribute>
<attribute w:name="domLabel_">
function () { return this.getLabel(); }
</attribute>
</radio>
i acheived this through a piece of code.
first create Outer Hlayout then radio button then inner hlayout
created two label's & then set css seperatly
set that lable to inner hlayout,
inner hlayout to radio
and radio to outerlayout.

I want to add source from property file

I have started using struts .I have hanged in a place ,Code is bellow
<st:submit src="getText('image.user.login')" type="image" height="21" width="44" </st:submit>
when i run this code , getText('image.user.login') message does return any value , But when i replace src="getText('image.user.login')" with value="getText('image.user.login')" than it returns value of "image.user.login" from property file.
What is reason for it , and how can i solve this issue ?
Thanks in advance
like this example illustrates in this reference submit reference struts
Render an image submit:
<s:submit type="image" value="%{'Submit'}" label="Submit the form" src="submit.gif"/>
src : Supply an image src for image type submit button. Will have no effect for types input and button.
AND
value : String Preset the value of input element.
this should answer your question if you use src you should assign a path and if you use value you can use a preset value
try this:
<st:submit key="image.user.login"/>

Categories