Read the label of a Radio Button and confirm the value with Selenium - selenium

So as the title suggests. I am looking to confirm that the value of the Radio button is correct.
The HTML is as follows:
<input type="radio" value="Coach" name="servClass" checked="">
<font face="Arial, Helvetica, sans-serif">
Economy class
<br>
<input type="radio" value="Business" name="servClass">
Business class
<br>
<input type="radio" value="First" name="servClass">
First class
</font>
The selenium bit is as follows:
String expectedServiceClass = "First class";
String actualServiceClass = driver.findElement(By.cssSelector("input[value='First']")).getText();
if (actualserviceClass.equals(expectedServiceClass)){
System.out.println("Correct Wording");
}else{
System.out.println("Oops: somethings not right with the wording");
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
But when this is executed, the actualServiceClass variable doesn't contain any values i.e. null therefore the "if statement" will always print "Oops: somethings not right with the wording"
Any help???

With the current HTML code, you won't be able to confirm the value of label of Radio button as Radio button is implemented as Input tag, that is a self-closing tag and hence getText() on input will always return null. You will need a container tag like div to include the Input tag(radio button) and the label. Refer: Self-closed versus Container Tags
The problem is not with the Selenium Code, its actually due to the improper HTML snippet. Changing the HTML as below can solve this:
<font face="Arial, Helvetica, sans-serif">
<div>
<input type="radio" value="Coach" name="servClass" checked="">
Economy class
<br>
</div>
<div>
<input type="radio" value="Business" name="servClass">
Business class
<br>
</div>
<div>
<input type="radio" value="First" name="servClass">
First class
</div>
After this, just changing the Css Selector or XPath to find the div will give you value of label of Radio Button. Css Selector can be div>input[value='First']. Let me know if you are able to solve the problem.

I agree with #Manu the HTML snippet is poor but you can use javascript childNodes to get the text from the nodes
The childNodes property returns a collection of a node's child nodes, as a NodeList object.
The nodes in the collection are sorted as they appear in the source code and can be accessed by index numbers. The index starts at 0.
Use executescript to execute JavaScript in the context of the currently selected frame or window
Below is an example in java
Don't forget to add return since you need to return the value to the caller
WebElement element = driver.findElement(By.xpath("//input[#value='Coach']/following-sibling::font"));
String node_text=(String)((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[0].nodeValue",element);
System.out.println(node_text.trim());
Try the above script it will return "Economy class"
In the above script we use childnode property to get all the childnodes of
font tag <font face="Arial, Helvetica, sans-serif">
similarly you can get the other text nodes by replacing childnode index
childNodes[4]----->"Business class"
childNodes[8]------>"First class"
I tried the above code it was working fine
Hope this helps you...kindly get back if you have any queries

Related

Webelement getText() function returns different values on the same element

Following code is used to get text of specified button element with id:
EE__printer-menu__activator
In our structure it's drop-down and here is element with child elements DOM structure:
<button data-v-12817ac3="" type="button" class="some classes" id="EE__printer-menu__activator"><div class="v-btn__content">
Printer
<i data-v-12817ac3="" aria-hidden="true" class="some classes">arrow_drop_down</i>
</div>
</button>
The problem is following: when drop-down is not expanded and I'm trying to get text of id="EE__printer-menu__activator" element using getText() I receive :
{Printer
arrow_drop_down}
which is expected. but when drop-down is expanded and I'm using the same I get only text of first child:
{Printer}
Can't understand why in this case getText() doesn't return "arrow_drop_down" text of the last child of selected element.
When drop down is not extended then if you pay attention
<i data-v-12817ac3="" aria-hidden="true" class="some classes">arrow_drop_down</i>
to more particular attribute aria-hidden is set to true. so when you are calling .getText on EE__printer-menu__activator, you are getting both the text.
Now, here is a bit assumption that when drop down is expanded, then aria-hidden value will be set to false. causing .getText() to return what it has, and it does have only first Printer.

Xpath for Submit button with precedence nodes select file and *

Query 1:
I would request you to please help me on getting xpath for submit button. Absolute path starts with Select file and *.
<div id="bg">
<label id="label" style=" font-family: Segoe UI;color:#2e2e2e; font-size:12px; float:left; padding-top:8px;">
Select File <span id="spanhide" class="red">*</span></label>
<div style="margin-left:105px;"><input type="file" name="filUploadIcon" id="filUploadIcon" class="txt-box" onchange="FileUpload_OnChange(this,event);" style="width:180px;">
<input type="submit" name="btnUploadcancel" value="" onclick="return check();" id="btnUploadcancel" title="Upload" class="upload_pop"></div>
<input name="textFileName" type="text" id="textFileName" style="display:none;">
<input type="hidden" name="hdnframeID" id="hdnframeID">
<input type="hidden" name="hdnlbl" id="hdnlbl">
</div>
Query 2:
How to write xpath to skip few nodes in between. Please help. Also let me know adding // or * in between to skip nodes.
Ex: Above HTML
//*div[#id="bg"]/skip elements before input type submit node/input [#type="submit"]
Use the below xpath to target the precedence nodes that include the label with the string "Select File" and the embedded span that contains '*'.
//div[contains(#id, 'bg')]/label[contains(text(), 'Select File')]/span[contains(text(), '*')]
Then add on the below line to return to the parent node label to the span tag.
/parent::label
Then add on the below to get to the sibling div tag of the label tag, which contains the input tag with a type of submit.
/following-sibling::div/input[#type='submit']
So the xpath in its entirety should look like this:
//div[contains(#id, 'bg')]/label[contains(text(), 'Select File')]/span[contains(text(), '*')]/parent::label/following-sibling::div/input[#type='submit']
You can use below xpath.
//div[#id='bg']//input[#name='btnUploadcancel']
Strongly suggest to go through this to learn more on xpath 1.0 which will answer all your questions in OP.

WebDriver - finding sibling elements

I have a html markup like this:
<html>
...
<body>
....
<fieldset>
<legend>Other Details</legend>
<div>
<input type='text' id='txt1' title='Detail 01'/>
</div>
<div>
<input type='text' id='txt2' title=''/>
</div>
<div>
<input type='text' id='txt3' title=''/>
</div>
<div>
<input type='text' id='txt4' title='Detail 04'/>
</div>
</fieldset>
.....
<body>
</html>
This is what I want to do.
I need to rapidly traverse the screen, pick up all the textboxes and their titles. When I encounter a textbox that has no title (like two of them in my above example), I need to check if they occur under a fieldset, which they do. Now, for these textboxes without a title, all I have to do is to take the legend value of the fieldset and assign it to the textbox.
For example, the second and third textboxes in the above example do not have a title. In this case I want to do the following:
Take the legend value of the containig fieldset (in this case, "Other Details")
Derive the position of the textbox in the fieldset and append it to the fielset legend like Other Details 2
Do the same thing for the third textbox.
I achieved step 1. When I am in step 2, I need to find out the position of the textbox in the parent fieldset. I have my selenium webdriver code below:
WebElement textbox = driver.findElement(By.id("txt2"));
List<WebElement> precedingSiblings =
textbox.findElement(By.xpath("preceding-sibling::input[#type='text' or #type='Text' or #type='TEXT']"));
String myTitle =
fieldSetLegend + " " + Integer.toString(precedingSiblings.size()+1);
I expect this code to give me the value of myTitle to be Other Details 2 and Other Details 3 for my second and third textboxes.
But the problem is, everytime I hit precedingSiblings.size(), it always returns 0. That is because each input is contained within a div and hence has no siblings.
Now, I want to know of a way through which I can find at what position my current element is within the containing fieldset.
Please help.!!!
What about navigating to the parent div finding it's preceding sibling & then finding the relevant input from there.
Roughly:
parent::div/preceding-sibling::div/input[#type='text' or #type='Text' or #type='TEXT']

How to check if radio button is selected or not using Selenium WebDriver?

<div class="my_account_module_content">
<h3 class="my_account_module_content_title">
Mi Dirección 1
<br>
<span>Predeterminada</span>
<div class="selectCard_left">
<input id="17390233" class="default_shipping_address" type="radio" name="address" checked="true">
<span>Seleccionar como tarjeta predeterminada</span>
</div>
this is the HTML code
If radio button selected is true then print the class span value?
please help me..
In Java, this would do it:
if(driver.findElement(By.id("17390233")).isSelected()){
System.out.println(driver.findElement(By.xpath("//input[#id='17390233']/following-sibling::span[1]")).getText());
}
If the radio button is selected, then the text will show. If you want to use the text somewhere, I suggest you put it in a string instead:
String spanText = driver.findElement(By.xpath("//input[#id='17390233']/following-sibling::span[1]")).getText();
Hope this answers your question.
EDIT: Here is an update of other ways to try.
If the className default_shipping_address is unique (e.g. not used anywhere else on the page), you may try locating the element by className:
if(driver.findElement(By.className("default_shipping_address")).isSelected()){
System.out.println(driver.findElement(By.xpath("//input[#class='default_shipping_address']/following-sibling::span[1]")).getText());
}
If that class is not unique, maybe the DIV's className selectCard_left is?
if(driver.findElement(By.className("selectCard_left"))){
System.out.println(driver.findElement(By.xpath("//div[#class='selectCard_left']/span[1]")).getText());
}
If none of the classNames are unique, a complete xpath expression is required. If you still are unable to get that text, I refer to reading up on how to use xpath: http://www.w3schools.com/XPath/xpath_syntax.asp
I hope that you find this information useful.

Selenium problem locating by DOM

I'm trying to use the DOM to locate a form element in Selenium but I can't get it to work. Even if I use the example in the Selenium documentation it still fails, for example with this html...
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
and this command in the Selenium IDE...
verifyElementPresent
with target...
dom=document.forms['loginForm']
I get [error] false in the log.
The 'getElementById' example in the documentation does work, but none of the others.
Can someone explain what I'm doing wrong here?
Thanks.
Not sure why it's not working (I can replicate the problem), but perhaps there's a better way to locate your target element? I would recommend locating by ID/name, falling back to CSS or XPath.
The format to locate a element is-
i) document.forms[index of the form].elements[index of the element]
index of the form = the index number (starting at 0) of the form with respect to the whole page, index of the element = the index number (starting at 0) of the element with respect to the whole form that contains it.
ii) document.forms[“name of the form”].elements[“name of the element”]
name of the form = the value of the name attribute of the form tag that contains the element you want to access, name of the element = the value of the name attribute of the element you wish to access
iii) document.getElementById(“id of the element”)
id of the element = this is the value of the ID attribute of the element to be accessed. This value should always be enclosed in a pair of parentheses (“”).
iv)document.getElementsByName(“name”)[index]
name = name of the element as defined by its ‘name’ attribute, index = an integer that indicates which element within getElementsByName’s array will be used.