gpt4 book ai didi

java - 在java中使用selenium web驱动程序从div获取文本值

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:46 25 4
gpt4 key购买 nike

我有这个html:

<div class="b-datalist__item__subj">
hello
<span class="b-datalist__item__subj__snippet">hello world</span>
</div>

我只想获取“hello”文本,没有“hello world”。我写这段代码:

subject = this.driver.findElement(By.xpath("//div[@class = 'b-datalist__item__subj']")).getText();

但在控制台输出中我看到“hellohello world”。我该如何修复它?

最佳答案

您必须从 div 中的完整文本“减去”span 内的文本。像这样的东西:

String outsideText = driver.findElement(By.xpath("//div")).getText();
String insideText = driver.findElement(By.xpath("//span")).getText();
String yourText = outsideText.replace(insideText, "");

根据您的具体情况调整 XPath。

关于java - 在java中使用selenium web驱动程序从div获取文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37863719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com