Selenium Java: How can I get the value - selenium

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.

Related

Selenium and capture object

Using firefox and marking a link in my web-app I get among other things, this code which I think I can use to caprture an object:
cb_or_somename_someothername cb_area_0219
This string is "classname" in Firebug.
Going to the script I type in:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219"));
But the script does not find the element when executing.
Other onfo in the Firebug panel is:
class="cb_or_somename_someothername cb_area_0219"
onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false"
href="#"
id="fwMainContentForm:j_idt156:2:selectRole"
Is my script referring the element in a wrong way?
You cannot use search by compound class name (name with spaces). Try to use search by CSS selector instead:
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219"));
or by XPath:
WebElement rolle = driver.findElement(By.xpath("//*[#class='cb_or_somename_someothername cb_area_0219']"));
Also you still can use search by one of two class names:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername"));
or
WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part
Update
If you get Element is not clickable... exception it seem that your element is covered by another element at the moment you try to click on it. So try to wait until this "cover" is no more visible:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[#class='environtment-banner']"));
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
rolle.click();

How to get the span class text using selenium webdriver

I have a span as below:
<div class="ag-cell-label">
<span class="glyphicon glyphicon-asterisk" title="This is a draft row. It can only be seen by you. "/>
</div>
I want to get the text "glyphicon glyphicon-asterisk". How can I do it.
The validation of the test case is to check weather asterisk is not present after clicking on save button.
Assuming you are using Java, You should try as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='ag-cell-label']/span")));
String class = el.getAttribute("class");
Hope it will help you...:)
This is a simple case of:
Locate the WebElement using a suitable locator strategy (Class, CSS, XPath etc) and assign it to a new WebElement object.
Use the .getAttribute(String arg) method with an argument of "class" to retrieve the required class value from the WebElement object instantiated in the first step and assign it to a new String object.
Use the .contains(String arg) method with an argument of "asterisk" to determine whether the "class" attribute retrieved in the second step contains the text "asterisk".

Modify innerHTML using Selenium

I have this element:
WebElement element = ...
string val = element.getAttribute("innerHTML");
All I want to do is to change this innerHTML on my web page.
Is it possible?
Try this:
WebElement element = ...
((JavascriptExecutor)driver).executeScript(
"var ele=arguments[0]; ele.innerHTML = 'my new content';", element);
Selenium WebDriver does not have any direct methods to do so to change the DOM itself. However we can use JavascriptExecutor to use javascript to modify the DOM.
check this example to change the background color. You will get an idea to change the innerHTML as well.
in python use this :
element = driver.find_element_by_id("some_id")
driver.execute_script("arguments[0].innerText = 'what_you_want_to_show'", element)

The method getAttribute("value") is not working for textbox

I am new to selenium. I am trying to retrieve the value of a textbox. Below is my code.
WebElement e = driver.findElement(By.id("id"));
e.sendKeys("text");
String str = e.getAttribute("value");
System.out.println(str);
The above code is working fine in all sites but is not working for a particular site. I can't share the site details.
Any explanation regarding why the code is not working for a site or is there another way to get the text from a textbox?
getAttribute('value') will provide you with null cause html snippet doesnot contains value attribute or the DOM object of that element has no value attribute. Try getting the text with :
String str = e.getText(); //If it helps
OR
use JavascriptExecutor as
JavascriptExecutor js = (JavascriptExecutor) driver;
String str = js.executeScript(
"return document.getElementById('company').value")
.toString();

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