Reputation: 31
I have to extract to total number of letters in this following example.
<div data-v-2f952c88="" class="text">
<section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="ABC" class="description__section"><!---->
<div data-v-051a83e7="" data-v-3b70ad5b="" class="markdown" data-v-2f952c88="">
<p>Headline 1<br>
This is my first example</p>
<p> Another Text
this is onother example
</p>
</div>
</section>
<section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="DEF" class="description__section">
<div data-v-051a83e7="" data-v-3b70ad5b="" class="markdown" data-v-2f952c88="">
<p>Headline 2<br>
Java Rocks</p>
<p> Another Text
Selenium also rocks
</p>
</div>
</section>
</div>
How can I extract all the letters inside the several tags "p" that are under several tags "section"?
Upvotes: 0
Views: 89
Reputation: 177
how are you? well... first of all do you read something about HTML DOM?
in Javascript Using DOM you can do something like this:
var myCollection = document.getElementsByTagName("p");
Next you will have something like an collection of "p" tags
You can access them by index number: y = myCollection[1];
or loop it:
var i;
for (i = 0; i < myCollection.length; i++) {
//do something with myCollection...
}
Your example can look something like:
var myCollection = document.getElementsByTagName("p");
var i;
var added = 0;
for (i = 0; i < myCollection.length; i++) {
added += myCollection[i].innerText.length;
}
alert(added);
<html>
<head>
</head>
<body>
<div data-v-2f952c88="" class="text">
<section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="ABC" class="description__section"><!---->
<div data-v-051a83e7="" data-v-3b70ad5b="" class="markdown" data-v-2f952c88="">
<p>Headline 1<br>
This is my first example</p>
<p> Another Text
this is onother example
</p>
</div>
</section>
<section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="DEF" class="description__section">
<div data-v-051a83e7="" data-v-3b70ad5b="" class="markdown" data-v-2f952c88="">
<p>Headline 2<br>
Java Rocks</p>
<p> Another Text
Selenium also rocks
</p>
</div>
</section>
</div>
</body>
</html>
I hope you find it useful!
Upvotes: 1
Reputation: 1778
What about this?
String[] tags = {"p", "section"};
int totalLetters = 0;
for (String tag: tags) {
List<WebElement> elements = driver.findElements(By.tagName(tag));
for (WebElement element: elements) {
totalLetters = totalLetters + element.getText().length();
}
}
Upvotes: 1
Reputation: 787
have you tried to iterate throught all elements something like this(didn´t look the java syntasys but adapt it for yourself jus take the idea)
foreach(IwebElement element in driver.findElements(By.Tag("p"))){
//Work with the element.Text
}
Upvotes: 2