Reputation: 31
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.
driver.findElement(By.xpath(contains(text(), "menu")));
I am using Eclipse Kepler and Selenium 2.39.0.
The exception i receive is : org.openqa.selenium.NoSuchElementException
.
I am not able to figure out where am i going wrong.
Upvotes: 0
Views: 8089
Reputation: 107247
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 '
. 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. For example, the following xpath:
driver.findElement(By.xpath("//*[contains(text(), 'menu')]"));
Will find the li
element with the text "menu" (note xpath is case-sensitive):
<html>
<foo>
<bar>
<ul>
<li id="123">menu</li>
</ul>
</bar>
</foo>
</html>
If possible, be even more certain, e.g. if you know that it is a li
element:
driver.findElement(By.xpath("//li[contains(text(), 'menu')]"));
Edit (OP put the actual html up)
This will find the DIV
element:
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.
Upvotes: 2
Reputation: 815
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:
WebElement we = driver.findElement(By.cssSelector("li:nth-child(1)")) //baring you know the index of the list
Upvotes: 1