英文:
Get the total number of letters in a text
问题
我必须提取以下示例中的字母总数。
如何提取位于多个标签“section”下的多个标签“p”中的所有字母?
英文:
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"?
答案1
得分: 2
你尝试过像这样迭代所有元素吗(我没有查看过Java的语法,但适应你自己,只是理解思路)
foreach(IwebElement element in driver.findElements(By.Tag("p"))){
//处理 element.Text
}
英文:
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
}
答案2
得分: 1
这是怎么样的情况?
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();
}
}
英文:
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();
}
}
答案3
得分: 1
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 a 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 another 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!
英文:
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:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var myCollection = document.getElementsByTagName("p");
var i;
var added = 0;
for (i = 0; i < myCollection.length; i++) {
added += myCollection[i].innerText.length;
}
alert(added);
<!-- language: lang-html -->
<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>
<!-- end snippet -->
I hope you find it useful!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论