<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.
Related
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);
<a ui-sref="apps.service" href="#/apps/service">
<i class="glyphicon glyphicon-list icon zoom-icon text-info-dker"></i>
<span class="font-bold"> Service</span>
</a>
How can I locate the element?
driver.findElement(By.partialLinkText("service")).click();
driver.findElement(By.xpath("ui[href$=services]"));
partialLinkText is case sensitive. Try
driver.findElement(By.partialLinkText("Service")).click();
The element is in <a> tag, not <ui> tag
driver.findElement(By.xpath(".//a[href*='services']"));
You should try using xpath with WebDriverWait to wait until element visible and enable as below :-
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[contains(.,'Service')]"))).click();
It is better to use contains in to locate element in this xpath. The below xpath will be accurate.
driver.findElement(By.xpath("//a/span[contains(text(),'Service')]")
This XPath,
//a[normalize-space() = 'Service']
will select all a elements whose normalized string value equals "Service" -- perfect for the example you show.
Be wary of solutions based upon contains() as they will also match strings that include additional leading or trailing text, which may or may not be what you want.
1
1
I want to do a click on the second element where the outside-month tag is missing. How can I achieve this?
Is there a special selenium function or do I have to write a special xpath expression?
For your scenario I imagine you could devise an XPath expression that uses the not() and contains() operators together.
Something along the lines of:
//a[not(contains(#class, 'outside-month'))]
I haven't tested whether this works or not but let me know if not and I can help refine it.
If outside-month is missing on target element you should use = with class name as below xpath:-
String xPath = "//a[#class = 'ng-binding ng-scope']";
or
if you want to use contains and target element is the last with containing class name you should pass last() as below :-
String xPath = "(//a[contains(#class, 'ng-binding ng-scope')])[last()]";
or
if you want to use contains and target element is not the last element with containing class name you should pass appropriate index as below :-
String xPath = "(//a[contains(#class, 'ng-binding ng-scope')])[2]";
or
if you want outside-month is missing but ng-binding ng-scope present you should try as below :-
String xPath = "//a[contains(#class, 'ng-binding ng-scope') and not(contains(#class, 'outside-month'))]";
Use anyone of the above xPath and perform click as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath)));
el.click();
Note :- Be ensure before finding element that it is not inside any frame. If it is inside any frame you need to switch that frame first as :- driver.switchTo().frame("frame name or id")
Hope it will help you..:)
in that element outside-month it's not a tag or attribute it's just a bigger class name
so what you need is to find a tag that have ng-binding ng-scope class name:
driver.findElement(By.xpath("//a[#class='ng-binding ng-scope']")).click();
I am quite new on Selenium (started today) and I would like to get the WebElement corresponding to the following html Input:
<input size="25" style="text-align:center;" value="http" onclick="this.select();" type="text"></input>
And then obtain its value. This is what I have tried so far:
WebElement element = driver.findElement(By.cssSelector(".text-align:center"));
String text = element.getText();
Or this:
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
But Java returns in both cases an exception:
org.openqa.selenium.InvalidSelectorException: The given selector
.text-align:center is either invalid or does not result in a
WebElement
Thank you,
Héctor
Do you have to search for the element by cssSelector?
You could give this a try:
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
If cssSelector is not necessary you could try grabbing the element by xpath.
If you use firefox, there is a plugin called FireBug which allows you to right click after inspecting the element and copying the xpath directly then using :
WebElement element = driver.findElement(By.xpath("XPATH HERE"));
EDIT: Part of post disappeared, redded it.
Your first try is slightly off
driver.findElement(By.cssSelector(".text-align:center"));
The (.) in a CSS selector indicates a CSS class name but that's a style on the element and not a class. There is no class on that element to use in that way.
Your second try looks good but maybe it's not unique on the page? Hard to tell with only the one line of HTML. You'd have to provide more of the HTML of the page. Try it again but get the value instead of text.
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
System.out.println(element.getAttribute("value"));
Does that work? You likely will have to provide some unique HTML that surrounds the INPUT that we can use to make the CSS selector more specific.
I would like to select the text inside the strong-tag but without the div under it...
Is there a possibility to do this with jsoup directly?
My try for the selection (doesn't work, selects the full content inside the strong-tag):
Elements selection = htmlDocument.select("strong").select("*:not(.dontwantthatclass)");
HTML:
<strong>
I want that text
<div class="dontwantthatclass">
</div>
</strong>
You are looking for the ownText() method.
String txt = htmlDocument.select("strong").first().ownText();
Have a look at various methods jsoup have to deal with it https://jsoup.org/apidocs/org/jsoup/nodes/Element.html. You can use remove(), removeChild() etc.
One thing you can do is use regex.
Here is a sample regex that matches start and end tag also appended by </br> tag
https://www.debuggex.com/r/1gmcSdz9s3MSimVQ
So you can do it like
selection.replace(/<([^ >]+)[^>]*>.*?<\/\1>|<[^\/]+\/>/ig, "");
You can further modify this regex to match most of your cases.
Another thing you can do is, further process your variable using javascript or vbscript:-
Elements selection = htmlDocument.select("strong")
jquery code here:-
var removeHTML = function(text, selector) {
var wrapped = $("<div>" + text + "</div>");
wrapped.find(selector).remove();
return wrapped.html();
}
With regular expression you can use ownText() methods of jsoup to get and remove unwanted string.
I guess you're using jQuery, so you could use "innerText" property on your "strong" element:
var selection = htmlDocument.select("strong")[0].innerText;
https://jsfiddle.net/scratch_cf/8ds4uwLL/
PS: If you want to wrap the retrieved text into a "strong" tag, I think you'll have to build a new element like $('<strong>retrievedText</strong>');