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!
Related
I am working with a pre-defined field so you can only enter specific values and if you enter any other value (which is not mapped in database) then it throws error.
Now, I want to check for which values (from excel sheet), this field throws error but I am struggling to achieve this because the only things which changes in the DOM for invalid value is ::before. If it is invalid ::before will appear.
Example:
<label for="Broom" data-error="Please." class="active">::before</label>
When I came across this problem, I could not see any way of doing this through the WebDriver locator strategy like XPATH, CSS etc. We can only use this via Javascript. Something like this:
public String errorCheck() {
String script = "return window.getComputedStyle(document.querySelector('label[for=\\'Broom\\']'),':before').getPropertyValue('content')";
JavascriptExecutor js = (JavascriptExecutor)driver;
String content = (String) js.executeScript(script);
return content;
}
So, you can verify if it returns null for valid values. If you do more research around this, you will find you can do more validations as well i.e colour etc.
This is more from reference point of view.
<div class="_2b9p7-6Tcy_ja6zEhxML2e">
<div class="flex _1VLiOJeD-kdBBLC9owCkQr">
<span class="flex-1">”Total””:”</span>
<span>
“USD”
“2,175”
</span>
Need to get the text inly in second quotes "", in this case "“2,175”", and I do not know how to do it.
Both “USD” and “2,175” comes from API (they are different every time, hence I can't use driver.findElement(By.xpath("xpath and not 'USD'), I can split the result, and take only the necessary part, but I would like to know a way how to take the value from only the second brackets for the future (2,175).
I had performed the following on Intellij
System.out.println(driver.findElement(By.xpath("//div[#class = '_2b9p7-6Tcy_ja6zEhxML2e']//div//span/following-sibling:: span")).getText());
and getting, of course, full text - USD 2,175, but I need 2,175
To extract the value 2,175 you need to induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:
Using xpath and following:
WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Total')]//following::span[1]")));
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString());
Using xpath and following-sibling:
WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Total')]//following-sibling::span[1]")));
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString());
driver.findElement(By.xpath("//div[#class = '_2b9p7-6Tcy_ja6zEhxML2e']//div//span/following-sibling:: span")).getText().substring(7, 11);
You can use substring in two ways:
String substring(int begIndex)
String substring(int beginIndex, intendIndex)
Hope this helps.
Use split on the returned string like shown below.
String[] amount = returnedText.split(" ");
amount[1] should be your required text.
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);
i want to get text All New Products and Launches i am trying this
driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2/span")).getText();
but it is printing Folder
<h2 class="iselmf-h2-icon">
<span>Folder:</span>
All New Products and Launches
</h2>
The text is in the <h2> tag, not the <span> tag. Try
driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2")).getText();
Two ways to do this,
1) driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2")).getText();
2) Get the text from both and then concatenate it, something like
String firstblock = driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2")).getText();
String secondblock = driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2/span")).getText();
String finaltext= firstblock+secondblock;
Look at the HTML tags
<span>Folder:</span>
The /span indicates that this is the end of the span, so if you're using the span as the identifier then it will only return folder.
The parent element of the span is the h2 tag. So to get everything between h2 and /h2 tags you need to do this -
driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2")).getText();
I had quite similar issue when getting the text from span attribute. The solution was to use .getAttribute("value") like this:
driver.findElement(By.xpath(".//*[#id='iselmf-folder-detail']/h2/span")).getAttribute("value");
**String org.openqa.selenium.WebElement.getAttribute(String name)**
I am trying to capture the product description from a webpage using ID and tagname but when I print it, it is showing blank. However I think I have used correct locators to locate the element.
Page Source
<div id="item-description-block" class="layout-container layout-container-background clearfix">
<h2>About this item</h2>
<div id="social_share" class="details-row" onclick="javascript:clickPDP('Share','123070751499');">
<div class="details-row clear_both">
<div id="inspirational_copy">
<p>The big plus: Our new formula now helps strengthen skin's own moisture barrier. More moisture stays in. Skin feels soft, springy. Has a healthy-looking glow.</p>
</div>
Web Driver Code
driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.id("inspirational_copy").tagName("p"));
String description1 = description.getText();
I just tested it in Python and if you replace By.id("inspirational_copy").tagName("p") with a valid css selector you can then use getText() to get the text you're looking for.
driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.cssSelector("div[id='inspirational_copy']>p"));
String description1 = description.getText();
I did notice when I arrived on the page I got a welcome message. This message prevented me from getting the text. After closing it I could get the element and the text without problems.
WebElement close = driver.findElement(By.cssSelector("button[title='Close']");
close.click()
You can solve this problem easily by using xpath as locator
driver.findelement(By.xpath("//div[#id='inspirational_copy']/p"));
String description1 = description.getText();
Hope this will get the text you want.
String description1 = driver.findElement(By.xpath("//*[#id="inspirational_copy"]/p")).getText();