我正在使用 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/