如何在 Selenium Webdriver (Java) 中使用 xpath 从嵌套的 div 中获取文本 [英] How to get text from nested div using xpath in Selenium Webdriver (Java)

查看:42
本文介绍了如何在 Selenium Webdriver (Java) 中使用 xpath 从嵌套的 div 中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从嵌套的 div 中选取文本,但在使用 xpath 作为定位器时无法获取文本.任何人都可以帮助和建议我如何从嵌套的 div 中获取文本.代码如下...

<div class="dijitPopup dijitTooltipDialogPopup" id="SMGGP_dropdown" role="presentation" style="RIGHT: auto;左:SO6px;Z-索引:1000;显示:无;顶部:S83px;可见性:可见"dijitPopupParent="><div tabIndex="-1" title="" class="dijitTooltipDialog dijitTooltipABLeft dijitTooltipAbove" id="SMGGP" role=presentation" style="WIDTH: 300px;位置: ;顶部:0px;可见性:可见widgetld=SMGGP">

解决方案

执行此操作的几种方法.一种简单的方法:获取具有 ID 的元素,然后通过 xpath 找到该元素中的 div:

(在 C# 中,但 Java 非常接近它)

var topDiv = _driver.FindElementById("SMGGP");var div = topDiv.FindElementByXPath("//div/div");

I want to pick text from from a nested div but I am unable to get the text while using xpath as locator. Can anybody help and advise how can I get text from nested div. Code shown below...

<div class="dijitPopup dijitTooltipDialogPopup" id="SMGGP_dropdown" role="presentation" style="RIGHT: auto; LEFT: SO6px; Z-INDEX: 1000; DISPLAY: none; TOP:S83px; VISIBILITY: visible" dijitPopupParent=">
 <div tablndex="-1" title="" class="dijitTooltipDialog dijitTooltipABLeft dijitTooltipAbove" id="SMGGP" role=presentation" style="WIDTH: 300px; POSITION: ;
TOP: 0px; VISIBILITY: visible" widgetld=SMGGP">
<div class="dijitTooltipContainer" role="presentation">
 <div class="dijitTooltipContents dijitTooltipFocusNode" role="dialog" data-dojo-attach-point=’containerNode">
Text that needs to be picked</div>

</div>
</div>
</div>

解决方案

Several ways to do this. One easy way: Get the element you have the ID for, then find the div within that element, by xpath:

(in C#, but Java is very close to it)

var topDiv = _driver.FindElementById("SMGGP");
var div = topDiv.FindElementByXPath("//div/div");

这篇关于如何在 Selenium Webdriver (Java) 中使用 xpath 从嵌套的 div 中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆