java - 如何通过 Selenium 根据 HTML 提取跨度内的文本 209.520?

标签 java selenium xpath css-selectors webdriver

我正在使用 selenium 进行自动化,并且尝试获取跨度标签内的值。我该怎么做?我已经尝试过 getText() 但打印出 null

这是 HTML 中的行

<span class="visible-xs" data-bind="html: PriceWithoutCurrencySymbol">209.520</span>

我需要调用 99.520。我已经创建了正确的 xpath 来找到它,但是如何提取该值?

感谢您的帮助。

如果 xpath 带来了多个对象,但我需要的是获取该值,

这是我使用的xpath

//div[@class='totalPrice']/span[@data-bind='html: PriceWithoutCurrencySymbol']

这是 HTML 代码

<div class="flightPriceContainer notranslate">
                                <div class="totalPrice">
                                    <span class="hidden-xs" data-bind="html: Price">COP 209.520</span>
                                    <span class="visible-xs" data-bind="html: CurrencySymbol">COP</span>
                                    <span class="visible-xs" data-bind="html: PriceWithoutCurrencySymbol">209.520</span>
                                </div>
                            </div>

最佳答案

根据您共享的 HTML,所需的元素是 React元素,因此要提取文本 209.520,您必须引发 WebDriverWait 以使元素可见,您可以使用以下解决方案之一:

  • css选择器:

    String labelText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.flightPriceContainer.notranslate > div.totalPrice > span.visible-xs[data-bind$='PriceWithoutCurrencySymbol']"))).getAttribute("innerHTML");
    
  • xpath:

    String labelText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='flightPriceContainer notranslate']/div[@class='totalPrice']/span[@class='visible-xs' and contains(@data-bind,'PriceWithoutCurrencySymbol')]"))).getAttribute("innerHTML");
    

关于java - 如何通过 Selenium 根据 HTML 提取跨度内的文本 209.520?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022088/

相关文章:

带有附加信息的 Java8 计数收集器

java - 无法使用 PhantomJS Webdriver 和 Java 提交表单

sql - 将循环数据插入sql

java - 无法在框架中找到元素

java - FacesContext.getCurrentInstance() 抛出 java.lang.VerifyError : Inconsistent stackmap frames at branch target exception

java - 如何在 ListView 项目中存储日期?

java - 有两个键的 TreeMap

python - xpath 之间的层次结构/依赖关系

java - 使用 NetBeans 对 Selenium 进行跨浏览器测试

xml - 用于将属性值与集合进行匹配的 XPath 查询