ai didi

java - 如何使用 Selenium 获取深度 div 标签内的文本

转载 作者:行者123 更新时间:2023-11-30 02:53:42 24 4
gpt4 key购买 nike

我正在尝试获取以下 2 个 div 元素内的项目的文本内容。它使用绝对路径工作:

element = driver.findElement(By.xpath(".//*[@id='page-wrapper']/section/section/div/div[2]/section/div/div/div/div[45]"));
data = element.getText();
System.out.println(data);

我想要获取的文本是:

45SeSelenium

I am trying to get text using relative XPath, but it always fails.
I am trying use this combination:

element = driver.findElement(By.xpath("//div[contains(@class,'elemBox noclaim noclaim-tt') and contains(text(),'45')]")); 

 element = driver.findElement(By.xpath("//div[contains(@class,'elemBox noclaim noclaim-tt')]/div[contains(text(),'45')]"));

但还是失败了。

我有这样的 HTML 代码:

<div class="elemBox noclaim noclaim-tt text-right m-t-lg animated-add pulse-add animated pulse animated-add-active pulse-add-active" >
<div class="" ng-show="(filterTool(elementName.name)) || (name === '')">
<div class="text-right ng-binding">45</div>
<div class="text-left ng-binding">
<span class="elemSym ng-binding">Se</span>
<br/>
Selenium
</div>
</div>

我该怎么办?

最佳答案

通过使用contains(text(),'45'),只会评估第一个直接子文本节点。这就是您尝试 XPath 未能找到 div 的原因;因为文本“45”从外部 div 嵌套了 2 层:

//div[contains(@class,'elemBox noclaim noclaim-tt') and contains(div/div/text(),'45')]

或者您可能想尝试使用 . 而不是文本来评估外部 div 内的整个文本,而不是仅评估第一个直接子文本节点:

//div[contains(@class,'elemBox noclaim noclaim-tt') and contains(.,'45')]

关于java - 如何使用 Selenium 获取深度 div 标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37874228/

24 4 0
文章推荐: java - Hibernate 查询在字符串字段中搜索特定子字符串
文章推荐: java - 在 testng 中并行化类和方法
文章推荐: android - 从 Activity 向上导航回到 fragment
文章推荐: java - 如何使用正则表达式替换字符?
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com