[英]Selenium WebDriver find object text. Errors out with The result of the xpath expression is: [object Text]. It should be an element
I'm using Selenium to verify the presence of certain text in a web page.我正在使用 Selenium 来验证 web 页面中是否存在某些文本。 This is how the html looks like.这就是 html 的样子。
<html>
<div class="a-content">
<!--!-->==$0
"
Text to Find"
<br>
<br>
"
Second Text to find"
<br>
</div>
The only way I have been able to identify this text is through xpath.我能够识别此文本的唯一方法是通过 xpath。
//div[contains(@class, 'a-content')]//br[1]/preceding-sibling::text()[1]
I'm not able to use the regular way of identifying this element at it returns an object Text instead of an element.我无法使用常规方式识别此元素,因为它返回 object 文本而不是元素。
var x = webDriver.FindElement(By.XPath("//div[contains(@class, 'a-content')]//br[1]/preceding-sibling::text()")).Text;
This is the error I get:这是我得到的错误:
OpenQA.Selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//div[contains(@class, 'a-content')]//br[1]/preceding-sibling::text()" is: [object Text]. OpenQA.Selenium.InvalidSelectorException:无效选择器:xpath 表达式“//div[contains(@class, 'a-content')]//br[1]/preceding-sibling::text()”的结果是: [对象文本]。 It should be an element.它应该是一个元素。
Alternately, I have tried using JavaScript Executer for finding this text object.或者,我尝试使用 JavaScript Executer 来查找此文本 object。 But when I run below code it returns null.但是当我运行下面的代码时,它返回 null。
IJavaScriptExecutor javascriptExecutor = (IJavaScriptExecutor)webDriver;
String value = (String)javascriptExecutor.ExecuteScript("document.evaluate(\"//div[contains(@class, 'a-content')]//br[1]/preceding-sibling::text()[1]\") ; ");
Any help on this is appreciated.对此的任何帮助表示赞赏。
You should be able to take out the "::text()" to get the web element (and then get the Text property):您应该能够取出“::text()”以获取 web 元素(然后获取 Text 属性):
var x = webDriver.FindElement(By.XPath("//div[contains(@class, 'a-content')]//br[1]/preceding-sibling")).Text;
adding the::text() is returning the text of the element you selected.添加::text() 将返回您选择的元素的文本。 This is a feature of xapth that doesn't fit into the WebElement expectation of findElement.这是 xapth 的一个特性,不符合 findElement 的 WebElement 期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.