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

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);

Related

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.

Unable to retrieve text 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());

Using Selenium to select text

Want to select the text "This is for testing selector" from below HTML code.
<div class="breadcrumb">
<a title=" Home" href="http://www.google.com/"> Home</a>
<span class="arrow">»</span>
<a title="abc" href="http://www.google.com/">test1</a>
<span class="arrow">»</span><a title="xyz" href="http://www.google.com/">test2</a>
<span class="arrow">»</span>
This is for testing selector
</div>
I'm not sure if there an easy way out for this or not. It turned out to be more difficult than I thought. Below mentioned code is tested locally and giving correct output for me ;)
String MyString= driver.findElement(By.xpath("//div[#class='breadcrumb']")).getText();
//get all child nodes of div parent class
List<WebElement> ele= driver.findElements(By.xpath("//div[#class='breadcrumb']/child::*"));
for(WebElement i:ele) {
//substracing a text of child node from parent node text
MyString= MyString.substring(i.getText().length(), MyString.length());
//removing white spaces
MyString=MyString.trim();
}
System.out.println(MyString);
Let me know if it works for you or not!
Try with this example :
driver.get("http://www.google.com/");
WebElement text =
findElement(By.className("breadcrumb")).find("span").get(1);
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
I suggest also that you copy the xpath for the text you need and put it here to have the exact xpath
You cannot select text inside an element using xpath.
Xpath can only help you select XML elements, or in this case, HTML elements.
Typically, text should be encased in a span tag, however, in your case, it isn't.
What you could do, however, is select the div element encasing the text. Try this xpath :
(//div[#class='breadcrumb']/span)[3]/following-sibling::text()
You could try Abhijeet's Answer if you just want to get the text inside. As an added check, check if the string obtained from using getText() on root element contains the string obtained from using getText() on the child elements.

How to get values from disabled inputs in webdriver

My html code is -
<input id="txtPortalLogin" class="form-control input-sm" type="text" disabled="disabled" placeholder="No Link" value=""/>
Please assist some code to get the values from disabled field.
Screenshot is attached so that you will find the which text values i am talking about.
Input Fields are disabled and I want values from shown screenshot
I know how to get this value with Python code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('your_page_url')
disabled_input_field = driver.find_element_by_id("txtPortalLogin")
value = disabled_input_field.get_attribute('value')
Use the javascript executor in selenium to execute a javascript code which return the value of the html element ( input). Something simmilar to the below
String value = "";
if (driver instanceof JavascriptExecutor) {
String value = (String)((JavascriptExecutor) driver)
.executeScript("return document.getElementById('txtPortalLogin').value");
}
In C# it would be
driver.FindElement(By.Id("txtPortalLogin")).GetAttribute("value");
however you could also do this using the JavaScript executor as so:
var value = ((IJavascriptExecutor)driver).ExecuteScript("return $('#txtPortalLogin').attr('value')").ToString();
EDIT; I think the OP might want to get the https://production etc part out of the field. I'm not 100% sure how to do that, sorry.
I did also find this link which might be able to answer your problem; TLDR is make it readonly instead of disabled which allows the value behind to still be accessible but not changeable by the user.
Usually driver.getAtribute("value"); works. But as value field is empty, you'll have to go for JavaScriptExecutor -
JavascriptExecutor je = (JavascriptExecutor) webDriver;
String value = je.executeScript("return angular.element(arguments[0]).scope().{{modalValue:put modal value from HTML}};", {{webElement}}).toString();
return value;

How to get the value of an attribute using XPath

I have been testing using Selenium WebDriver and I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my regression testing. But I couldn't find a good answer.
Here is my sample html element:
<div class="firstdiv" alt="testdiv"></div>
I want to get the value of the "alt" attribute using the XPath. I have an XPath to get to the div element using the class attribute which is:
//div[#class="firstdiv"]
Now, I am looking for an XPath code to get the value of the "alt" attribute. The assumption is that I don't know what is the value of the "alt" attribute.
You can use the getAttribute() method.
driver.findElement(By.xpath("//div[#class='firstdiv']")).getAttribute("alt");
Using C#, .Net 4.5, and Selenium 2.45
Use findElements to capture firstdiv elements into a collection.
var firstDivCollection = driver.findElements(By.XPath("//div[#class='firstdiv']"));
Then iterate over the collection.
foreach (var div in firstDivCollection) {
div.GetAttribute("alt");
}
Just use executeScript and do XPath or querySelector/getAttribute in browser. Other solutions are wrong, because it takes forever to call getAttribute for each element from Selenium if you have more than a few.
var hrefsPromise = driver.executeScript(`
var elements = document.querySelectorAll('div.firstdiv');
elements = Array.prototype.slice.call(elements);
return elements.map(function (element) {
return element.getAttribute('alt');
});
`);
Selenium Xpath can only return elements.
You should pass javascript function that executes xpaths and returns strings to selenium.
I'm not sure why they made it this way. Xpath should support returning strings.