简体   繁体   English

如何为没有标签的元素识别xpath?

[英]How to identify xpath for an element which does not have any tag?

I would need to identify one element uniquely.However that element does not have any tag.Could you please help to identify the element uniquely? 我需要唯一地标识一个元素。但是该元素没有任何标签。请帮助您唯一地标识该元素吗?

<div class="mbs2_support ng-binding"><strong>Customer care? </strong>Phone 999 888 777 6</div>

Here I need to identify only phone number column without "Customer care" section. 在这里,我只需要识别电话号码列,而无需“客户服务”部分。 Highlighted one(attached screenshot) I need identify.I have tried following xpath, but it is taking whole row(including customer column) 高亮显示的(附有屏幕截图)我需要确定。我尝试了以下xpath,但是它占用了整行(包括客户列)

  • //div[contains(text(), ' Phone 999 888 777 6')]
  • //div[text()[normalize-space()='Phone 999 888 777 6']]

在此处输入图片说明

"...element does not have any tag" is not actually correct. “ ...元素没有任何标签”实际上是不正确的。 Required text node is a child of div . 必需的文本节点是div的子节点。 You can get it with below JavaScript (using JavaScriptExecutor ): 您可以使用下面的JavaScript(使用JavaScriptExecutor )获得它:

'return document.querySelector("div.mbs2_support").lastChild.textContent;'

只需提取diff的text() 子代<strong>的文本就是孙代,因此将不包括在内:

//div[contains(., 'Phone ')]/text()

The element you are trying to identify is a child text Node of the parent <div> tag. 您尝试识别的元素是父<div>标记的子text Node

As per the HTML you have provided to extract the text Phone 999 888 777 6 you can use the executeScript() method as follows: 根据您提供的用于提取文本Phone 999 888 777 6HTML ,您可以如下使用executeScript()方法:

WebElement myElement = driver.findElement(By.xpath("//div[@class='mbs2_support ng-binding']"));
String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString();
WebElement element = driver.findElement(By.xpath("//div[contains(@class,'mbs2_support')]")); 
String getText = ((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', element).toString();

Get the element and parse the string. 获取元素并解析字符串。 lastChild in javascript will do the trick: https://www.w3schools.com/jsref/prop_node_lastchild.asp javascript中的lastChild可以解决这个问题: https ://www.w3schools.com/jsref/prop_node_lastchild.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 想用JSP核心标识元素 <c:if> 使用Xpath或其他任何定位器使用Selenium Webdriver标记 - want to identify element with JSP core<c:if> tag using selenium webdriver using xpath or any other locators 如何在Selenium WebDriver中标识没有唯一标识符/属性的元素? - How to identify an element in Selenium WebDriver that does not have any unique identifier/property? 即使xpath相同,如何识别硒中的元素 - How to identify an element in selenium even if xpath is same 为一个没有任何唯一 ID 的元素编写 Xpath - Xpath writing for one element which doesn't have any unique id 如何使用 Xpath、css 或 Selenium 中的任何其他定位器在 html 中的结束标记后查找带有“== $0”的元素 - How to find element with "== $0" after end tag in html using Xpath, css or any other locators in Selenium 任何标签外的元素的getText(),而无需在xpath中使用text() - getText() of an element outside any tag without using text() in xpath 如何使用 Selenium 和 Java 构建 xpath 或 css 定位器来识别元素 - How to construct a xpath or css locator to identify the element using Selenium and Java 如何通过Java Selenium中的XPath标识文本元素为Google登录 - How to identify the element with text as Sign in with Google through xpath in java selenium 如何在 xpath 中传递变量字符串以识别 Selenium 和 Java 中的元素 - How to pass a variable string within the xpath to identify an element in Selenium and Java 即使所有匹配节点的xpath相同,如何识别硒中的元素 - How to identify an element in selenium even if xpath is same for all the matching nodes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM