简体   繁体   中英

Selenium WebDriver — get <span> text

I need to get the price value $726.35 from:

<span id="paiement-resultats"class="calculateur-resultats-total" style="" xpath="1">$726.35</span>

but it doesn't work:

driver.findElement(By.id("paiement-resultats")).getText()

How can I get this value?

Screenshot from the browser:

浏览器的屏幕截图

Try this one,

Below i have Mentioned Two Xpath Try With that, if not working Let Me Know

//div[@id='resultats']//following::span[@id='paiement-resultats']
//div[@id='resultats']//following::span[@class='calculateur-resultats-total']

driver.findElement(By.xpath("//h2[@class='resultats']//following::span[@id='paiement-resultats']")).getText()

As the element is within a <span> tag, to extract the text $726.35 you can use either of the following solution:

  • cssSelector :

     driver.findElement(By.cssSelector("span.calculateur-resultats-total#paiement-resultats")).getText(); 
  • xpath :

     driver.findElement(By.xpath("//span[@class='calculateur-resultats-total' and @id='paiement-resultats']")).getText(); 

Update

As the results were unstable you can induce WebDriverWait for the visibilityOfElementLocated and you can use either of the following solutions:

  • cssSelector :

     String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[aria-label='User']"))).getAttribute("innerHTML"); 
  • xpath :

     String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[contains(@style,'vertbar')]"))).getAttribute("innerHTML"); 

Cheers! I found the solution

driver.findElement(By.xpath("//span[@class='calculateur-resultats-total' and contains(text(),'$')]")).getText();

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