Selenium WebDriver get text from input field - selenium

I'm writing a test to assert the default text value within an <input> tag. However, it's not playing ball:
Assert.assertThat(webDriver.findElement(By.id("inputTag")).getText(), Matchers.is("2"));

This is the input element - you need to get the value attribute:
webDriver.findElement(By.id("inputTag")).getAttribute("value")

The most reliable solution I found, is executing a JavaScript script and return the value attribute of the HTMLInputElement from JavaScript.
I have tested this in several ways and the value was always correct, even after changing the input field value.
PHP
$input = $driver->findElement( WebDriverBy::id( 'inputTag' ) );
$value = $driver->executeScript( 'return arguments[0].value', $input );
// A custom PHP function that returns the value of an input field:
function getValue(RemoteWebDriver $driver, WebDriverBy $by ) {
return $driver->executeScript(
'return arguments[0].value',
$driver->findElement( $by )
);
}
// Sample usage:
$value = getValue( $driver, WebDriverBy::id( 'inputTag' ) );
Java
//driver being your WebDriver
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement inpElement = driver.findElement(By.id("inputTag"));
String text = (String) js.executeScript("return arguments[0].value", inpElement);

Related

Get error [object Text]. It should be an element

Trying to get the text from the div with xPath. Finds information in the browser good, but when i try to run with an idea than i get error:"is: [object Text]. It should be an element."
List<WebElement> priceGameWebElement = webDriver.findElements(By.xpath("//div[contains(#class,'search_price')]" +
"/text()[normalize-space()][1]"));
What do I need to do to make everything work?
You can "interrupt" your query before the /text()... part like this:
List<WebElement> priceGameWebElement = webDriver.findElements(By.xpath("//div[contains(#class,'search_price')]"));
Then you should get a List<WebElement> which contains the elements with the text() nodes for further distinction. They could probably be queried with the .getText() function.
If for some reason you cannot exclude text() from XPath and you need just extract the text of the element, then there is a workaround: use method document.evaluate() from JavaScript.
Code example:
String textObjectXpath = "\"//*[local-name() = 'text'][#x='8']/text()\"";
String script = "var element = document.evaluate("
+ textObjectXpath
+ ", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"
+ "if (element) {return element.nodeValue;}";
String extractedText = (String) ((JavascriptExecutor)driver).executeScript(script);
The if statement "if (element) {return element.nodeValue;}" could be omitted if you don't care about the scenario when the element will be without text. In a such case, you may want to replace the line with "return element.nodeValue;"

Incrementing value with Selenium IDE

How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}

Get the value from xpath in selenium

How can I get the value from xpath in Selenium.
public void DummyFunc(String startDate){
String test;
test=driver.findElement(By.xpath("//startDate"));
}
I am passing the location of the table like table > tr > td but I am getting an error when I use String.
Your answer need more precision. If "by value" you mean "text", you can have it with getText() method (it return the innerHTML):
public void DummyFunc(String startDate){
String test;
test = driver.findElement(By.xpath("//"+startDate));
return test.getText()
}
edit :
btw, table > tr > td is not a valid xpath string. It should be //table/tr/td

How to get style sheet property value by webdriver?

I want to know whether content menu of drop down is displayed or none. I want to click on that menu if it's display none. and keep as it is if blocked. the property value is like this:
div id="section_content_23" class="thread-content" style="display: none;" i have xpath of title is like ex 'section_title_23'. i just want to know the style is "display: none;" or style = "display: blocked";
I have write code for this like:
else if (str.contains("section_title_"))
{
//String xpath;
String[] retval = str.split("_", 3);
String no = retval[2];
//
displaylinkhandler = QuickCap.driver.findElement(By.id("section_content_" + no)).getAttribute("style");
//displaylinkhandler = QuickCap.driver.findElement(By.id("section_content_" + no)).getAttribute("section_content_"+no);
//displaylinkhandler = QuickCap.driver.findElement(By.xpath(xpath)).getText();
//boolean show = displaylinkhandler.contains("display: none;");
if(displaylinkhandler == null){
QuickCap.driver.findElement(By.xpath(str)).click();
}
indexBoolean = TitleCheck.validate();
}
here str is xpath & displaylinkhandler is like string.
Thanks in advance.
did you tried getCssValue from webelement.
for instance: ele.getCssValue("propname")
By the way to check element is displayed or not you should user isDisplayed method of webelement
In webdriver we use
WebElement element = driver.findElement(By.name("elementName"));
and then
String attributeValue = element.getAttribute("attributeName");
So in your case it will
String styleValue = element.getAttribute("style");

Getting text from a node

I have a piece of HTML like this:
<a href="/something">
Title
<span>Author</span>
</a>
I got a WebElement that matches this HTML. How can I extract only "Title" from it? Method .getText() returns "Title\nAuthor"...
You can't do this in the WebDriver API, you have to do it in your code. For example:
var textOfA = theAElement.getText();
var textOfSpan = theSpanElement.getText();
var text = textOfA.substr(0, textOfA.length - textOfSpan.length).trim('\n');
Note that the trailing newline is actually part of the text of the <a> element, so if you don't want it, you need to strip it.
Here is the method developed in python.
def get_text_exclude_children(element):
return driver.execute_script(
"""
var parent = arguments[0];
var child = parent.firstChild;
var textValue = "";
while(child) {
if (child.nodeType === Node.TEXT_NODE)
textValue += child.textContent;
child = child.nextSibling;
}
return textValue;""",
element).strip()
How to use in this:
liElement = driver.find_element_by_xpath("//a[#href='your_href_goes_here']")
liOnlyText = get_text_exclude_children(liElement)
print(liOnlyText)
Please use your possible strategy to get the element, this method need an element from which you need the text (without children text).
If using Python:
[x['textContent'].strip() for x in element.get_property('childNodes') if isinstance(x, dict)]
Where element is your element.
This will return ['Title', ''] (because there are spaces after span).
you can use jsexecutor to iterate the child nodes, trap the textNode 'Title' and then return its content like below
WebElement link = driver.findElement(By.xpath("//a[#href='something']"));
JavascriptExecutor js = ((JavascriptExecutor)driver);
String authorText = (String) js.executeScript("for(var i = 0; i < arguments[0].childNodes.length; i++) {
if(arguments[0].childNodes[i].nodeName == \"#text\") { return arguments[0].childNodes[i].textContent; } }", link);
The javascript code block above iterates both textNode ('Title') and SPAN ('Author') but returns only the text content of textNode.
Note: Previous to this, I have tried including text node in xpath like below, but webdriver throws invalidselector exception as it requires element not textnode
WebElement link = driver.findElement(By.xpath("//a[#href='something']/text()"));
Verify the element present for "//a[normalize-space(text())=Title]". It will return true if the text present inside 'a' tag is 'Title'.