从下面的代码中我想获取文本“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/