[英]How to retrieve the text Effective Date from the text node within th node using Selenium and Java
[英]How do I write xpath to extract a date from a text node using Selenium and C#
<dd>
::before
<strong>Test Date:</strong>
"
7/6/20 - Monday"
::after
</dd>
以上是样品 html。
我想找到“测试日期:”之后的日期,因为页面中会有很多日期,我想参考文本“测试日期:”来定位日期。我如何为这个要求写 xpath? 或使用任何其他定位器。
写 xpath 是不够的(在你的情况下是//dd/strong[text()='Test Date:']/following-sibling::text()[1]
)。 您需要一个单独的文本节点,Selenium 不能将其视为 web 元素。 使用IJavaScriptExecutor
:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string dateText = (string)js.ExecuteScript("return document.evaluate(\"//dd/strong[text()='Test Date:']/following-sibling::text()[1]\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.cloneNode(true).text;");
文本7/6/20 - Monday位于父<dd>
节点的文本节点内。 因此,要提取测试日期之后的日期:您可以使用以下基于xpath的定位器策略:
IWebElement element = driver.FindElement(By.XPath("//dd[./strong[text()='Test Date:']]"));
string text = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].childNodes[3].textContent;", element);
您可以在以下位置找到一些相关的讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.