简体   繁体   English

Selenium WebDriver —获取<span>文本</span>

[英]Selenium WebDriver — get <span> text

I need to get the price value $726.35 from: 我需要从以下价格获取$ 726.35的价格:

<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 下面我提到了两个Xpath,如果不起作用,请尝试使用“让我知道”

//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: 由于元素位于<span>标记内,因此要提取文本$ 726.35 ,可以使用以下任一解决方案:

  • cssSelector : cssSelector

     driver.findElement(By.cssSelector("span.calculateur-resultats-total#paiement-resultats")).getText(); 
  • xpath : 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: 由于结果是不稳定的,你可以诱导WebDriverWaitvisibilityOfElementLocated,您可以使用以下两种解决方案:

  • cssSelector : cssSelector

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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