Unable to retrieve text selenium - selenium

Script:
List<WebElement> addCheck=driver.findElements(By.name("ticketLess"));
for(WebElement checkbox : addCheck ){
System.out.println(checkbox.getAttribute("Text"));
}
HTML:
<input type="checkbox" name="ticketLess" value="checkbox">
<font face="Arial, Helvetica, sans-serif" size="2">
Same as Billing Address </font>
The text i am trying to get is Same as Billing Address. I tried using getText() also but its not returning nothing.

List<WebElement> addCheck = driver.findElements(By.xpath(".//input[#name='ticketLess']/following-sibling::font"));
for(WebElement checkbox : addCheck ){
System.out.println(checkbox.getText());
}
The selector used above selects next sibling of input, that is <font>. You need to get the font element to retrieve the text you want.

Actually you're getting wrong attribute name on wrong element to getting text. input element doesn't contain inner text.
You need to locate font element because text is present inside font element and use .getText() to getting this text as below :-
List<WebElement> addCheck = driver.findElements(By.cssSelector("input[name = 'ticketLess'] + font"));
for(WebElement checkbox : addCheck ){
System.out.println(checkbox.getText());
}
Note :- If there are multiple checkboxes with the same locator and you want to get all text of these checkboxes font then you should use above code, otherwise if you can get text only this single checkbox font using findElement instead as below:-
WebElement checkbox = driver.findElement(By.cssSelector("input[name = 'ticketLess'] + font"));
System.out.println(checkbox.getText());

Related

How to write selenium code for autocomplete text box withe list selection

I'm trying to write Selenium code for below HTML source code..
This field is the auto populated field for input selection
<input id="ctl00_ContentPlaceHolder1_txtBranch" class="textbox_service ui-autocomplete-input" name="ctl00$ContentPlaceHolder1$txtBranch" style="width: 200px;" onblur="return branch();" onchange="return CheckBranchName();" tabindex="6" autocomplete="off" type="text"/>
Any one can help me out to write the code?
Web element screen shot attached.
Thanks in advance.
This is the best I could do with the information you provided. If you could show the HTML for what the autocomplete list looks like that would be great. You didn't specify any language so I'll assume it's Java.
WebElement field = driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtBranch"));
field.click();
field.sendKeys(Keys.SPACE);
List<WebElement> items = driver.findElements(By.tagName("li");
for (int i=0; i<items.size();i++) {
WebElement elementYouWantToClick = items.get(i);
String x = elementYouWantToClick.getText();
if(x.contains("TextThatIsInYourElementYouWantToChoose")){
elementYouWantToClick.click();
}
Best I could do for now with such limited information.
As per the HTML to click(select) the Auto Complete text, you can use the following line of code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='textbox_service ui-autocomplete-input' and contains(#id,'_ContentPlaceHolder') and contains(#name,'txtBranch')]"))).click();

How to Send Text to Text Box which have same xpath

In Selenium Webdriver we have three Text boxes. All the text boxes have the same Id, and I want to send some text in the second text box.
This is my code:
Driver.findElements(By.xpath("//*[#id='testInstanceScan']"));
Could anyone please help how to handle Text boxes which have same Id ?
Currently i m using below code which always send same text for all the three text boxes.
List<WebElement> textfield1 = Driver.findElements(By.xpath("//*[#id='testInstanceScan']"));
for(int i=0; i<textfield1.size();i++){
WebElement local_textfield1=textfield1.get(i);
String value1=local_textfield1.getAttribute("placeholder");
if(value1.equalsIgnoreCase(""))
{
local_textfield1.sendKeys("Amarendra Singh");
}
}
There are two ways to get the second textbox.
Method 1: using find elements
List<WebElement> textfields = Driver.findElements(By.xpath("//*[#id='testInstanceScan']"));
textfields.get(1).sendKeys("Amarendra Singh");
Method 2: using xpath
WebElement textfield2 = Driver.findElements(By.xpath("(//*[#id='testInstanceScan'])[2]"));
textfield2.sendKeys("Amarendra Singh");
You can use an array like trick to access each element matching the XPath
WebElement textField1 = driver.findElements(By.xpath("(//*[#id='testInstanceScan'])[1]"));
WebElement textField2 = driver.findElements(By.xpath("(//*[#id='testInstanceScan'])[2]"));
The quickest and easiest way I know to do this is to use an XPath to specify the expected ID and placeholder = "" and then specify the index of the element you want.
Given an HTML example of
<html>
<div id="testInstanceScan" placeholder="">e1</div>
<div id="testInstanceScan" placeholder="">e2</div>
<div id="testInstanceScan" placeholder="">e3</div>
</html>
The code below works
List<WebElement> searchBox = driver.findElements(By.xpath("//div[#id='testInstanceScan'][#placeholder='']"));
System.out.println(searchBox.get(1).getText());
and returns
e2
which is the element that contains the desired ID, empty placeholder attribute value, and is the second element. This form of XPath can be tailored to a lot of different situations to get the desired element in a single pass instead of looping or filtering the collection.
In your case, your final code would look like
List<WebElement> textfields = driver.findElements(By.xpath("//input[#id='testInstanceScan'][#placeholder='']"));
textfields.get(1).sendKeys("Amarendra Singh");
If that still doesn't work, you may need a wait.
By locator = By.xpath("//input[#id='testInstanceScan'][#placeholder='']");
List<WebElement> textfields = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
textfields.get(1).sendKeys("Amarendra Singh");
I don't know exactly what is yoyr question but what i understand you want to perform operation on textbox which have same xpaths
This is a sample Html which I used for example
<html>
<div id="test" placeholder="">element1</div>
<div id="test" placeholder="">element2</div>
<div id="test" placeholder="">element3</div>
<div id="test" placeholder="">element4</div>
</html>
First use findElements() to extract all elements and then use if loop to select one and perform operation This is my code
WebDriver driver=new FirefoxDriver();
driver.get("URL");
List<WebElement> allElements=driver.findElements(By.xpath("//div[#id='test']"));
for (WebElement tempElement : allElements) {
if(tempElement.getText().equalsIgnoreCase("element2"))
{
System.out.println(tempElement.getText()); // you can perform your operations
}
}

Selenium Java: How can I get the value

I have a Catalogue Number text field, which has a value = Pen populated.
When I inspect the element in DOM tree, the textfield value is data bind.
How can I get the data bind value and verify the textfield is not empty using Selenium Java?
<input id="txtCatalogueNo" class="k-textbox" maxlength="25" data-bind="value: selectedCatalogue.CatalogueNumber">
Thanks
Try,
WebElement TxtBoxContent = driver.findElement(By.id("txtCatalogueNo"));
System.out.println(TxtBoxContent.getAttribute("value"));
or
WebElement TxtBoxContent = driver.findElement(By.id("txtCatalogueNo"));
System.out.println(TxtBoxContent.getText());
You can get the value of any attribute of a WebElement using getAttribute method.
If data-bind attribute contains the value,
WebElement TxtBox = driver.findElement(By.id(txtCatalogueNo));
System.out.println(TxtBox.getAttribute("data-bind")); // to get the value in data-bind attribute.
If Value attribute contains the value:
System.out.println(TxtBox.getAttribute("value")); // to get the value in data-bind attribute.
you can retrieve the test using getText method
System.out.println(TxtBox.getText()); // to get the text
Try this:
WebElement TxtBox = driver.findElement(By.id("txtCatalogueNo"));
String valueTxtBox=TxtBox.getAttribute("data-bind").split(":")[1].trim();
System.out.println(valueTxtBox);
OR
WebElement TxtBox = driver.findElement(By.id("txtCatalogueNo"));
String valueTxtBox=TxtBox.getAttribute("data-bind").replace("value: ", "");
System.out.println(valueTxtBox);
valueTxtBox will contain the value you are looking for.
Sorry for the late reply on this its pretty simple. I'll just assume you are using angular.
WebElement element= driver.findElement(By.id("txtCatalogueNo"));
String content = (String) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].value", element);
Hopefully this works for you.

Getting the default value (text) of an <input> element with selenium webdriver

I wonder about how I can use selenium webdriver to find the default text of an element ?
In the browser, the input field displays a default value: 'Project 1', but I cannot get this text through the method getText() of this WebElement.
<input class="title viewData" id="sprojectName" maxlength="255" name="projectName" type="text" projectinfo="1">
getText() returns "the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace." You need something like getAttribute("value") or getAttribute("placeholder").
The getText() method is for retrieving a text node between element tags for example:
Eg:
<p>New</p>
But usually the value in the text box is saved to "value" attribute. So the below statement will work:
findElement(By.id("ElementID")).getAttribute("value");
Yes, I will try to see if getAttribute("value") work. In the meantime, I have solved the problem using JavaScript executor:
String jsStatement = "return document.getElementById('" + elementId + "')." + "value" + ";";
JavascriptExecutor js = null;
if (session instanceof JavascriptExecutor) {
js = (JavascriptExecutor)session;
}
return (String) js.executeScript(jsStatement);

Issue selecting Radio button in Selenium Webdriver

<table id="Content_Content_Content_ctlCaseInfo_rdochldplcm" class="fltLeft">
<tr>
<td><input type="radio" id="Content_Content_Content_ctlCaseInfo_rdochldplcm_0" name="ctl00$ctl00$ctl00$Content$Content$Content$ctlCaseInfo$rdochldplcm" value="0" /><label for="Content_Content_Content_ctlCaseInfo_rdochldplcm_0">No</label></td><td><input type="radio" id="Content_Content_Content_ctlCaseInfo_rdochldplcm_1" name="ctl00$ctl00$ctl00$Content$Content$Content$ctlCaseInfo$rdochldplcm" value="1" /><label for="Content_Content_Content_ctlCaseInfo_rdochldplcm_1">Yes</label></td>
</tr>
</table>
When I try
driver.FindElement(By.Id("Content_Content_Content_ctlCaseInfo_rdochldplcm")).Click();
it clicks to "Yes"
When I try driver.FindElement(By.Id("Content_Content_Content_ctlCaseInfo_rdochldplcm_0")).Click();
OR
driver.FindElement(By.Id("Content_Content_Content_ctlCaseInfo_rdochldplcm_1")).Click();
Nothing happens and no radio button gets selected.
Please suggest ways to handle this situation ..thanks a lot!!
It would probably be better to click the Radio buttons through XPath.
In your specific case, the XPath for:
Yes - Radio Button:
"//input[contains(#id, 'rdochldplcm') and contains(#value, 1)]"
No - Radio Button:
"//input[contains(#id, 'rdochldplcm') and contains(#value, 0)]"
In this instance, if you wanted to click the 'Yes' Radio button, you can do this:
string yesRadioButtonXPath = "//input[contains(#id, 'rdochldplcm') and contains(#value, 1)]"
IWebElement yesRadioButton = driver.FindElement(By.XPath(yesRadioButtonXPath));
yesRadioButton.Click();
For the 'No' Radio button, you would use this:
string noRadioButtonXPath = "//input[contains(#id, 'rdochldplcm') and contains(#value, 0)]"
IWebElement noRadioButton = driver.FindElement(By.XPath(noRadioButtonXPath));
yesRadioButton.Click();
Since you're using a table, there may be a chance that the XPath may return more than one element. You'd need to use a different method to sort out the elements in that case, but for what you're looking for, this method should work.
this solved my problem perfeclty
I have a page with 18 radio buttons in 6 groups which represented "Yes" "No" and "No Answer"
I was trying to get them by ID but it was randomized by the app
But using a name and value tags made it work.
radios were defined basically like this:
input value="2" class=" x-form-radio x-form-field" autocomplete="off" id="randID_13578" name="emailNotifiyOptionAllow" type="radio">
and every time i opened this page id was different so using
"//input[contains(#name, 'emailNotifyOptionAllow') and contains(#value, 1)]"
solved it.
Thanx
Use this :
//First get the list of values from the radio button
List < WebElement > elements = driver.findElements(By.cssSelector("table[id='Content_Content_Content_ctlCaseInfo_rdochldplcm'] > td"));
WebElement value;
//use loop for searching the particular element
for(WebElement element : elements){
//Getting the value of the element
value = element.findElement(By.cssSelector("label")).getText();
//condition to click on the element
if(value.trim().equals("No")){ //Here value is hard coded. You can take from excel sheet also
// If condition satisfies, it will click on the element
element.findElement(By.cssSelector("input").click();
}
}
This can be used as a common function also.
try [0] and [1] instead of the underscore.
Try your code with the given below CSS :
Step 1:
By Provided HTML Piece we can derive the CSS of the Radio Button
css=#Content_Content_Content_ctlCaseInfo_rdochldplcm input
Step 2:
Click on the radio button using Web Driver Code
driver.findElement
(By.cssSelector("#Content_Content_Content_ctlCaseInfo_rdochldplcm input"))
.click();