How can I get the inner text of an element in Selenium? - java

I'm working with a DOM node:
<input
type="form-control"
type="text"
data-bind="textInput: EnterpriseId"
disabled
autocomplete="off">
How can I get its value? I'm struggling since element.getText() does not work and returns a blank.

Try this:
WebElement element = driver.findElement(By.id("id value"));
String val = element.getAttribute("innerText")

I presume the element in question is an <input> element, so you may be able to use the element.getAttribute(String attribute) method like so:
String value = element.getAttribute("value");

This input tag is disabled, hence element.getText() returns a blank value.
Use element.getAttribute("textContent") instead.

You may be looking for the placeholder of an input text, because you might try:
element.getAttribute("placeholder");

You can go to your browser → open developer tools → inspect element you want to take attribute from → click Properties → check if that value is in InnerText.
Then do as it is mentioned in previous comments:
element_locator.get_attribute('InnerText')

I had the exact same issue! This post solved it for me:
How can I get the current contents of an element in webdriver
I used:
element = driver.find_elements_by_xpath(
'//button[#class="size-grid-dropdown size-grid-button"]')
element.text

As other's suggested, HTML's input nodes don't have a text attribute because they can store data in multiple formats in a value attribute.
This can be easily seen in the HTML input API specification where this form control can be of type radio, date, file upload and many more.
So, in your specific case, I'd suggest you check the webdriver's API for a method that's able to retrieve the value attribute.

As a bonus to evaluate innerText of an element within Selenium:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourEl")));
wait.until(ExpectedConditions.attributeToBe(By.id("yourEl"), "innerText", yourValue));
Documentation: attributeToBe

It works definitely, as I've tested it several times:
<input type="form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">
In your example, you don’t have any innerText. So you can only get attributes as mentioned before with the existing attributes. In your case:
type, data-bind, EnterpriseId and autocomplete. No value will be as this attribute isn’t created.
If you want to get only existing, this should be fine:
String example = driver.findElement(ByLocator(("")).getAttribute("any attribute of your input");
System.out.println(example);

Related

Can't find checkbox element by name contains, using Selenium Java

I am trying to click on a checkbox that only has a name.
Here is the HTML:
<td class="checkbox-column"><input type="checkbox" name="link-active[138]"></td>
Here is my latest attempt to find the element, I have tried a lot of different methods. I also need to grab it without the "[127]" part since that is dynamic in this case.
driver.findElement(By.xpath("//*[text()[contains(.,'link-active')]]")).click();
You are using wrong xpath, correct xpath is:
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
You need to check the name attribute.
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
xpath has a name attribute method, you can try this
driver.findElement(By.xpath("//*[contains(name(),'link-active')]")).click();
You can simply introduce webdriverwait to let your script knows that it is visible and enabled and then you can try to click. Like this :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#name,'link-active')]"))).click();

How would I select this element using Selenium in Java?

I am trying to select for the value 1352 in Java Selenium on ChromeDriver
<span class="numfound" id="yui_3_18_1_1_1522936314968_15">1352</span>
Because the id is nonintuitive, I'd like to select using the String "numfound". I've tried selecting byClassName("numfound") and this was returned:
<[[ChromeDriver: chrome on MAC (befee42078624a3b036869cf2a4a0c14)] -> class name: numfound]>
Alternatively, I've tried to select by CSS and got this:
Unable to locate element: {"method":"css selector","selector":"#resultsnum span.numfound"}
Perhaps my selector for CSS was wrong? What would be the most intuitive way to select this element using numfound?
RESOLVED: I was silly and didn't use .getText() for what I wanted.
This span is a WebElement. There are certain things that you can do with WebElement. Some of those are :
1. click on it. (Provided that element must be clickable)
2. getText() : Text between the <span> and </span> tag.
3. getSize();
4. getLocation();
5. getScreenShotAs(OUTPUT.Type)
6. getRect();
7. SendKeys(charSequence) (Provided that it can take input something).
and many more.
As of now, in your problem, you can get the text between span tag.
by using this code :
String spanText = driver.findElement(by.cssSelector("span[class="numfound"]")).getText();
and do String operations on it.
Let me know if you have any concerns about this.
You can use the By-selector only for elements inside of the tag.
To get the text of an element
you can use
driver.findElement(By.xpath("//span[#class='numfound']")).getText();
or (if you like more):
driver.findElement(By.className("numfound")).getText();
or get it from the page source by
String source = driver.getPageSource();
and extract a string from this, starting with "numfound" and ending with following tag
Then extract your string from this line.
You just have to do:
WebElement element = browser.findElement(By.className("numfound"));
//do whatever you want with your element, get attributes, etc.
For reference: https://www.seleniumhq.org/docs/03_webdriver.jsp#by-class-name

Unable to replace value in a table column

Unable to send values in the table column. I got different errors while I am trying to insert a value in the column using Selenium.
I tried to set a new value in the table's column. It shows the error as The element must be user-editable in order to clear it.
WebElement.clear();
WebElement.sendKeys("value");
(or)
WebElement.sendKeys(Keys.DELETE);
WebElement.sendKeys("value");
Then to click and edit the value.
Actions actions = new Actions(getWebDriverEx());
WebElement TableColumn = Driver.findElement(By.id("element"));
actions.moveToElement(TableColumn);
actions.click().build().perform();
actions.sendKeys(Keys.BACK_SPACE+b+b);
actions.sendKeys("value");
The value which was passed is not inserted in the Tables column. But I can able to click the Tables column. Here my test passed.
Then tried to set value. It shows the error as timed out.
WebElement.sendKeys(Keys.DELETE);
WebElement.sendKeys("15000");
Again I used the div/span combination as XPath and I have edited the value. But it does not reflect in the table.
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("document.getElementById('element').innerHTML="+15000);
Here I do not get any errors. But the value not reflected after save.
I gave element to various formats.
div//[id]
div//span
XPath
id alone (which was in the div)
HTML:
<div id="element" class="tables_second_column">
<div class="class_name">
<div class="class_name">
<div class="class_name"><span>5000</span></div>
</div>
</div>
</div>
Try on the following and it's working for me:
js = "document.querySelector('#element .class_name .class_name .class_name>span').innerHTML = '15000';"
driver.execute_script(js)
Hope it helps you!
This should be sufficient
WebElement textBox = driver.findElement(By.xpath("//span[text()='5000']"));
textBox.clear();
textBox.sendKeys("");
textBox.sendKeys("15000");
I do send a empty space in order to get the textbox active as sometime the DOM might not reflect immediately as this element is quiet nested.
As per the HTML you shared, the table column is within a <span> which is inside several <div> tags. Hence we need to construct an unique xpath to identify the WebElement and first send clear() method then use sendKeys() method as follows :
driver.findElement(By.xpath("//div[#id='element']/div[#class='class_name']/div[#class='class_name']/div[#class='class_name']/span[text()='5000']")).clear();
driver.findElement(By.xpath("//div[#id='element']/div[#class='class_name']/div[#class='class_name']/div[#class='class_name']/span[text()='5000']")).sendKeys("15000");
Update
As you mentioned Text which was passed in the span is not predictable so we would simply omit the clause [text()='5000']. As you have provided a sample HTML with sample classnames I have constructed a nearly absolute xpath. So our code will be :
driver.findElement(By.xpath("//div[#id='element']/div[#class='class_name']/div[#class='class_name']/div[#class='class_name']/span")).clear();
driver.findElement(By.xpath("//div[#id='element']/div[#class='class_name']/div[#class='class_name']/div[#class='class_name']/span")).sendKeys("15000");
Thanks for your reply
It works for me with the below method
webElement.sendKeysByAction(value)

Selenium: how to get the value of hidden element which has all div tags

I would like to get the value of all div tags specified in attached. I have tried with all possible locators like classname etc, which is showing null. and tried with JavaScript also which is returning null.
Please see the screen shot and I need the selected text which is in blue color starts with "Enables enterprise IT to deploy networking services"
You need to research creating selectors as this isn't a difficult one. There are numerous approaches for this element, but here's one for you: $$("#offers-popover .description"). Obviously this is a CSS selector based on the $$ and you use getText from the Selenium API in order to scrape the element text, which is what I assume you are intending to do.
driver.findElement(By.css("#offers-popover .description")).getText();
Since your element is not visible you can try this:
String divText = driver.findElement(By.className("description")).getAttribute("textContent");
Or, if this is not the only element on the page with the class description:
WebElement popElement = driver.findElement(By.id("offers-popover"));
String divText = popElement.findElement(By.className("description")).getAttribute("textContent");

Selenium, verfiy that field formats correctly

I'm new to selenium and was hoping to get some help with this. Imagine that we have a box in which the user can write; I need to assert that the text field correctly formats the field for 1000000.00 = $1,000,000.00
I'm trying to use selenium and java to verify this. The field is represented by the html below.
<div id="req8">
<h2>Test #8</h2>
<input id="req8input" type="text" value="Type Number Here">
</div>
How would I go about this in Java? Thanks!
use this to get the correct text format for your value and then you can do an assert on the existing text value of the field. You can make the pattern specific to the locale as well. There are various other options. Something like this should work.
String value=""1000000.00""
DecimalFormat myFormatter = new DecimalFormat("###,###.##");
String output = myFormatter.format(value);
WebElement webElement= driver.findElement(By.ById("req8input"));
webElement.sendKeys("100);
assertEquals(output, webElement.getAttribute("value"));
To locate the element:
WebElement el = driver.findElement(By.id("req8input"));
To set the value of a text box:
el.sendKeys("1000000");
To read back the value:
String val = el.getAttribute("value");
To compare it:
assertEquals("$1,000,000.00", val);
Seeing as you said you are new to Selenium, this is probably going to lead to huge amount of questions. So to answer your next question(s): start with the documentation!

Categories