获取文本中的字母总数

huangapple go评论74阅读模式
英文:

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 = {&quot;p&quot;, &quot;section&quot;};
	int totalLetters = 0;
	for (String tag: tags) {
		List&lt;WebElement&gt; 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(&quot;p&quot;);

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 &lt; 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(&quot;p&quot;);
var i;
var added = 0;
for (i = 0; i &lt; myCollection.length; i++) {
   added += myCollection[i].innerText.length;
}
alert(added);

<!-- language: lang-html -->

&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div data-v-2f952c88=&quot;&quot; class=&quot;text&quot;&gt;
 &lt;section data-v-3b70ad5b=&quot;&quot; data-v-2f952c88=&quot;&quot; data-content-provider=&quot;ABC&quot; class=&quot;description__section&quot;&gt;&lt;!----&gt;
   &lt;div data-v-051a83e7=&quot;&quot; data-v-3b70ad5b=&quot;&quot; class=&quot;markdown&quot; data-v-2f952c88=&quot;&quot;&gt;
     &lt;p&gt;Headline 1&lt;br&gt;
       This is my first example&lt;/p&gt;
     &lt;p&gt; Another Text
        this is onother example
     &lt;/p&gt;
   &lt;/div&gt;
 &lt;/section&gt;
 &lt;section data-v-3b70ad5b=&quot;&quot; data-v-2f952c88=&quot;&quot; data-content-provider=&quot;DEF&quot; class=&quot;description__section&quot;&gt;
     &lt;div data-v-051a83e7=&quot;&quot; data-v-3b70ad5b=&quot;&quot; class=&quot;markdown&quot; data-v-2f952c88=&quot;&quot;&gt;
        &lt;p&gt;Headline 2&lt;br&gt;
            Java Rocks&lt;/p&gt;
        &lt;p&gt; Another Text
            Selenium also rocks
        &lt;/p&gt;
    &lt;/div&gt;
  &lt;/section&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

I hope you find it useful!

huangapple
  • 本文由 发表于 2020年10月13日 21:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64336075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定