[英]How to get a text of elements in div tag having same class name
there are 38 div tags in my web application and i want to get the text present in each div tags.following is the code:- 我的Web应用程序中有38个div标签,我想在每个div标签中显示文本。以下是代码:-
<div class="w-100 dt pa2 f7">
//this is parent div class<div class="w-100 dt pa2 f7">
//这是父div类
<div class="bg-white-10">
//sub class<div class="bg-white-10">
//子类
<div class="dib bg-dirty-green w-100">
//sublcass<div class="dib bg-dirty-green w-100">
// sublcass
<p class="pa2 ma0 fl f6">
// class from where i want to capture the text<p class="pa2 ma0 fl f6">
//我要从中捕获文本的类
<span class="v-mid">Mega Contest - 20</span> </p>
//this is the text which i want to capture.<span class="v-mid">Mega Contest - 20</span> </p>
//这是我要捕获的文字。
I tried with following code:- 我尝试使用以下代码:-
WebElement wb = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[3]/div[3]")); //CONTEST_TABLE
List<WebElement> list = driver.findElements(By.xpath("//*[@class='pa2 ma0 fl f6']")); //NUMBER_OF_CONTEST
int rc=list.size();
System.out.println(rc);
for(int i=0;i<rc;i++)
{
WebElement Contest_Name= list.get(i).findElement(By.xpath("//*[@class='pa2 ma0 fl f6']"));
String name = Contest_Name.getText();
System.out.println(name);
}
i am getting "Mega Contest - 20" 38 times...
我参加了38次“超级竞赛-20” ...
There is no need to retrieve element in List Array, You can get it single time: 无需检索列表数组中的元素,您可以一次性获得它:
//span[@class='v-mid']
if span class has Unique name, //span[@class='v-mid']
如果span类具有唯一名称,
OR if its common class name you can use with following parameter of xpath condition: //p[@class='pa2 ma0 fl f6']//following::span[@class='v-mid']
或者,如果可以使用它的通用类名称,则可以将其与xpath条件的以下参数一起使用:
//p[@class='pa2 ma0 fl f6']//following::span[@class='v-mid']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.