[英]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 6的HTML ,您可以如下使用
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.