java - 如何使用 xpath 或 css 选择器提取标题属性和文本?

标签 java selenium xpath css-selectors

从下面的代码中我想获取文本“Manager”。 第一行和第三行包含“经理”一词。

<a id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_a" class="level2" treenode_a="" onclick="" target="_blank" style="" title="Manager" xpath="1">
<span id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_ico" title="" treenode_ico="" class="button ico_close" style="width:0px;height:0px;"></span>
<span id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span" class="node_name">Manager</span>
</a>

我尝试使用以下 3 个 xpath,但没有一个给出文本“Manager”。

(xpath ="//span[@id='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span']/text())
(xpath ="//*[@id=\"HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_a\"]")
(xpath ="//span[@id='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span']")

还想从以下代码中获取单词“Employees”。

<input id="HieararchyDropdownMenu" class="selectedHierarchyValue" type="text" readonly="" value="Employees" xpath="1">

如有任何帮助,我们将不胜感激。

最佳答案

要获得经理的头衔,请尝试此操作。

String title=driver.findElement(By.xpath("//a[@class='level2']")).getAttribute("title");
System.out.println(title);

要获取文本管理器,请尝试以下 xpath。

String text=driver.findElement(By.xpath("//a[@class='level2' and contains(@title , 'Manager')]//span[@class='node_name']")).getText();
System.out.println(text);

要获取员工,您需要使用 getAttribute("attributename")

String idval=driver.findElement(By.id("HieararchyDropdownMenu")).getAttribute("value");
System.out.println(idval);

关于java - 如何使用 xpath 或 css 选择器提取标题属性和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59271140/

相关文章:

java - Java的静态全局变量在函数中使用时会改变其值而不返回吗?

java - 如何在 Selenium 的 Xpath 中转义双引号?

python - 如何获取 Span 标签之间的文本 XPATH Python

xpath - 从 Ext JS 应用程序中识别 xpath

html - XPath:如何选择以下 sibling 直到某个 sibling

java - 如何更新我的 JRE?

java - Eclipse 中的 JFace 和断言 ClassNotFoundException

java - 无参数构造函数与带参数的构造函数

testing - 在 Play Framework 自动测试模式( headless 浏览器)中使用时,click/clickAndWait selenese 命令失败

java - 带有页面对象模型页面工厂的 Selenium 网格