Selenium java code: How to read text from hidden element that contain many tags - selenium-webdriver

1.<svg blablabla style:overflow: hidden > <aaa id=aaa>
2.+
3.<g>
4.<g>
5.<g>
6.<g>
The tags are inside the svg.
1 is the hidden tag, i want to get the 5th line text(), this is what i do.
WebElement hiddenDiv = driver.findElement(By.id("aaa"));
String n = hiddenDiv.getText();
String script = "return arguments[0].innerHTML";
n = (String) ((JavascriptExecutor) driver).executeScript(script, hiddenDiv);
System.out.println(n);
how can i get g[2], i have tried by direct xpath and it got an error because the svg is hidden.

You just need to grab an array of the g tags and pick the one you want. If you want the 5th, you would use the code below.
List<WebElement> gs = driver.findElements(By.cssSelector("#aaa g"));
System.out.println(gs.get(4).getAttribute("innerHTML"));

Related

How to get all the element count under the div with selenium

I am trying to get the count of the search results returned in MakeMyTrip application by searching the flights from Hyderabad to Bangalore. By using the below I am able to get the text but how to verify how many number of search results returned.
String output = driver.findElement(By.xpath("//*[#id=\"left-side--wrapper\"]/div[3]")).getText();MakeMyTrip Flight Search
System.out.println(output);
Thanks in Advance
You should use driver.findElements(); method like this below:
// your webelement
By eachSearchElement = By.xpath("//*[#id='left-side--wrapper']/div[3]");
// getting all of available elements on the page and store them in List
List <WebElement> allSearchElements = driver.findElements(eachSearchElement);
// then just simply get the size of particular List above
int howManyElements = allSearchElements.size();
System.out.println("There are " + howManyElements + " present on the page");
Hope this will help.

How do I get the text from the li tag

How do I get the text from the li tag? I want to find the text "Password is required." only, not the text inside strong tag.
<li><strong>Error:</strong> Password is required.</li>
You need to show your code for somebody to give a complete answer. I guess that you already know how to do something like the following
WebElement something = driver.FindElement(By.CssSelector(?))
string s = something.Text;
The next bit seems to be where you are stuck. There you need to parse the string s. That is nothing to do with Selenium-Webdriver. You could do something like
string[] s2 = s.split(new string[] {">","<"});
were the last element in s2 would be your answer here. This would be totally non generic though. Is this a situation in which you always want to purge html?
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("//li")
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).

How to split a Elements in scala into a Array of Strings

I have some troubles into slitting an Element (scraped from the web) into an Array of Strings.
Here is my code :
link = "http://www.myurl.com"
val doc: Document = Jsoup.connect(link).get()
val title2 = doc.select("li > h3 > a").toString
that give me :
1ERE COMPAGNIE D'ARC DU DAUPHINÉ
38SMS
40 BATTEURS
4L FOUR LIBERTY
A BORD PERDU
what i want is to have only the href in a Array of Strings. Only take the strings in the " ".
I've try to use JavaConverters like asScala, but i'm falling working with it :/
Thanks
simply extract href attributes from the a you get like:
doc.select("li > h3 > a").map(link -> link.attr("href")).toArray
take a look the more attribute extracting features from Jsoup

How to select p and q tag in jsoup such that if same element is under the both the tags,it should be selected once?

How to select p tag and b tag both and if p and b have same element then only one should be selected.
<html>
<div>
<p><b>This is first line</b></p>
<b>This is second line</b>
<span style="color:blue">This is third line</span>
</div>
</html>
How do I select all the three lines only once?
If I use html.select("p,b");, <p><b>This is first line</b></p> gets selected twice.
You need the pseudo-selector :not to exclude the child b tag.
for (Element e : doc.select("p :not(b),b"))
System.out.println(e.ownText());
Output
This is first line
This is second line
To include the third line, add span to your selector.
for (Element e : doc.select("p :not(b),b,span"))
System.out.println(e.ownText());
Output
This is first line
This is second line
This is third line
How do I select all the three lines only once?
Use the parent with a selector for child nodes div>* (see CSS reference):
Update: use selector for elements, that are not <div> but a have a <div> parent .select("div>:not(div)")
String htmlString = "<html><div><p><b>This is first line</b></p><b>This is second line</b><span style=\"color:blue\">This is third line</span></div></html>";
Document doc = Jsoup.parse(htmlString);
Elements elements = doc.select("div>:not(div)");
for (Element element : elements) {
System.out.println(element.toString());
}
This prints out:
<p><b>This is first line</b></p>
<b>This is second line</b>
<span style="color:blue">This is third line</span>
If you will only use the text inside the nodes simply use element.text()

How to get text of element but excluding the sub-elements text

I want to get the text of the element without including the text of its elements. I have tried getText(), but it returns text that includes all the child elements text.
In the following example: When I retrieved text from the first div, it returns text that includes all its subelements.
<div class="row”>
<div class="col-lg-4 section”>
<div class="col-md-12”>
inseam 28 30 32
</div>
</div>
<div class="col-lg-5 section”>
<div class="col-md-13”>
inseam 28 34 36
</div>
</div>
</div>
Please let me know how to do this using webdriver in java.
Thanks
sean
I've been searching for the same thing for a while, here's my solution for those who can specify a WebElement or a list of WebElements:
def remove_child_text_from_webelement(webelement):
# Declaring the current text for this webelement
current_text = webelement.text
# Getting its childs elements in a list
childs_list = webelement.find_elements_by_xpath('./*')
# Manipulating text to remove child text from parents
childrens_text_list = [child.text for child in childs_list]
#return (childrens_text_list,type(childrens_text_list))
for children_text in childrens_text_list:
match_index = current_text.find(children_text)
if match_index != -1:
match_length = len(children_text)
current_text = current_text[0:match_index] + current_text[match_index+match_length:]
return current_text
Now you can do something like:
[remove_child_text_from_webelement(e) for e in browser.find_elements_by_xpath('//div[contains(#class,"person")]')]
When I retrieved text from the first div with class 'row', it returns text that includes all its subelements.
This happened because you retrieved text from the parent div and hence all the innerHTML/text of the child divs were retrieved along with them.
Below is the way to retrieve the necessary innerHTML/text only:
1- for 'inseam 28 30 32':
String text = driver.findElement(By.xpath("//div[#class='col-md-12']")).getText();
OR
String text = driver.findElement(By.className("col-md-12")).getText();
2- for 'inseam 28 34 36':
String text = driver.findElement(By.xpath("//div[#class='col-md-13']")).getText();
OR
String text = driver.findElement(By.className("col-md-13")).getText();
Not tried it specifically with Selenium, but with jQuery you can use contents() to get all elements including raw text nodes, filter by nodeType 3 (text nodes) and then take the first, in your example:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/p33gcfk2/1/
var text = $('.row').contents().filter(function () {
return this.nodeType == 3;
}).first();
alert(text.text());
This is happening because you are trying to get the text of parent tag. If you want to get tag of particular child, you have to reach all the way there. You can take use of "nth-child" or "nth-of-type". For e.g in this case, if you want to have return this text "inseam 28 34 36".
The CSS selector will be "div.row div:nth-of-type(3)" or you can directly specify the div class "div.col-md-13"
You can refer to this article on more on selectors https://saucelabs.com/resources/selenium/css-selectors

Resources