[英]By.xpath(contains(text(), “Some text”) gives error
The following code says that "The method text() is undefined for the type Test" and prompts me to create a new function text() in class Test. 下面的代码说“类型Test的方法text()未定义”,并提示我在Test类中创建一个新的函数text()。
driver.findElement(By.xpath(contains(text(), "menu")));
I am using Eclipse Kepler and Selenium 2.39.0. 我正在使用Eclipse Kepler和Selenium 2.39.0。
The exception i receive is : org.openqa.selenium.NoSuchElementException
. 我收到的异常是: org.openqa.selenium.NoSuchElementException
。 I am not able to figure out where am i going wrong. 我无法弄清楚我要去哪里错了。
XPath
expressions need to be surrounded by quotes - and since the expression you are trying to parse also contains a string literal, I would suggest you switch the literal to single apostrophe '
. XPath
表达式需要用引号引起来-并且由于您要解析的表达式还包含字符串文字,因此建议您将文字切换为单撇号'
。 Also, unless you expect the root element to contain the text menu
, you'll need to be more specific about the element you are searching for. 另外,除非您希望根元素包含文本menu
,否则您将需要更详细地了解要搜索的元素。 For example, the following xpath: 例如,以下xpath:
driver.findElement(By.xpath("//*[contains(text(), 'menu')]"));
Will find the li
element with the text "menu" (note xpath is case-sensitive): 将找到带有文本“ menu”的li
元素(注意xpath区分大小写):
<html>
<foo>
<bar>
<ul>
<li id="123">menu</li>
</ul>
</bar>
</foo>
</html>
If possible, be even more certain, eg if you know that it is a li
element: 如果可能,请更加确定,例如,如果您知道它是li
元素:
driver.findElement(By.xpath("//li[contains(text(), 'menu')]"));
Edit (OP put the actual html up) 编辑 (OP放上实际的 html)
This will find the DIV
element: 这将找到DIV
元素:
driver.findElement(By.xpath("//DIV[@style[contains(., 'menubar_menubutton.png')]]"));
Note that xpath is case sensitive - so you'll need to duplicate the SHOUTCASE tags. 请注意,xpath区分大小写-因此,您需要复制SHOUTCASE标记。
i feel there is a much easier way to do this. 我觉得有一种更简单的方法可以做到这一点。
i'd personally just do: 我个人只是做:
WebElement we = driver.findElement(By.Id("123"))
or if you'd like to leverage css selectors you could do: 或者,如果您想利用CSS选择器,可以执行以下操作:
WebElement we = driver.findElement(By.cssSelector("li:nth-child(1)")) //baring you know the index of the list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.