简体   繁体   中英

How to get text from inside tag use selenium webdriver java?

Here my HTML:-

<li id="li1">
  <div>
    <p class="font11 metadata_value">0 Views | 14 Downloads</p>
    <p class="font11 metadata_value">639.78 MB</p>
</div>                      
</li>

I want to get 639.78

How to get use selenium java?

I wrote this code but not working.

System.out.println(driver.findElement(By.xpath(".//[@id='li1']/div[1]/p[2]")).getText());

You can get the text using the code :

String text = driver.findElement(By.cssSelector("p.font11.metadata_value")).getText()

The variable text will contain 639.78 MB value . You can extract 639.78 from the text in several ways like :

text.split("\\s+")[0] // this will return 639.78

OR

you can use regular expressions for extracting number like ([0-9.]*).*

you can take help from this link if you are using regex

For having multiple <p> like :

<li id="li1"> <div> <p class="font11 metadata_value">0 Views | 14 Downloads</p> <p class="font11 metadata_value">639.78 MB</p> </div> </li> <li id="li1"> <div> <p class="font11 metadata_value">0 Views | 14 Downloads</p> <p class="font11 metadata_value">639.78 MB</p> </div> </li> ,

you can use xpath to get the text values.

driver.findElement(By.xpath("//li[@id='li1']/div/p[1]")).getText() // for first <p>

driver.findElement(By.xpath("//li[@id='li1']/div/p[2]")).getText() //for second <p>

driver.findElement(By.xpath("//li[@id='li1']/div/p[3]")).getText() // for third <p>

and so on ..

After getting the text of <p> tag , you can extract the info using some regular expression.

Using For loop on all available text using css selector, Can print it.

public void verifrytext(String inputext)
{
    WebDriver driver=new ChromeDriver();
    List <WebElement> text = driver.findElements(By.cssSelector(".font11 metadata_value"));

   for(int i=0; i< text.size(); i++)
   {
     if(text.get(i).getText().equalsIgnoreCase(inputext))
     {
       System.out.println(text.get(i).getText());
     }   
   }
}

Based on Selenium Tutorial :

public class Mytest1 { 
//To open Firefox browser 
WebDriver driver = new FirefoxDriver(); 

@Before public void beforetest() { 
//To Maximize Browser Window 
driver.manage().window().maximize(); 
//To Open URL In browser 
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html"); 
} 



@After public void aftertest() { 
   driver.quit(); 
   } 

   @Test public void test() { 
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

   //READ
   String datentime = driver.findElement(By.className("metadata_value")).getText();
   //WRITE
   driver.findElement(By.className("metadata_value")).sendKeys("SOME_STRING");

   }

 }

I would use a regular expression to remove the text around the number and then parse it:

String text = driver.findElement(By.cssSelector("#li1 > div > p:nth-child(2)")).getText();
Float value = Float.parseFloat(text.replaceFirst(".*?([\\d.]+).*", "$1"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM