By.xpath(contains(text(), "Some text") gives error - java

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.

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(., 'm‌​enubar_menubutton.png')]]"));
Note that xpath is case sensitive - so you'll need to duplicate the SHOUTCASE tags.

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

Related

Unable to click on a "text" link in google search page

I am trying to click on News link on google search page the HTML structure looks like this
I tried following xpaths but none worked
//a/child::span[1][contains(.,'News')]
The following xpath resulted in invalid selector: The result of the xpath expression "//a/child::span/following-sibling::text()[contains(.,'News')]" is: [object Text]. It should be an element.
//a/child::span/following-sibling::text()[contains(.,'News')]
Thanks
//a[contains(.,'News')] might return this link, but may result in a list of more than one element that you'd need to handle and select the right element from.
You can use Selenium's SearchContext to specify a container element, or solve it using an xpath one-liner like: //div[#role='navigation']//a[contains(.,'News')] (Effectively searching for a link that contains 'News' somewhere in it's html-tree, somewhere inside a div that has a role attribute with value 'navigation').
You simply need
//a[contains(., "News")]
Note that "News" is not a part of span, but a, so your 1st XPath won't work

Selenium WebDriver Java locating an element with dynamic ID

I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.
What i have right now is the element of Variables (8) //a[contains(.,'Variables (8)')]. What i need is only the "Variables" since the number 8 is always changing.
Any thoughts on this? Any ideas will be much appreciated. Thanks
First of all 'Variables (8)' is not Id, its text. Id's are not dynamic since they represent unique identifier for the web element. This will look like this (based on your example):
<div class="field" id="fieldId">
As for your question, you can find the element by partial linked text:
driver.findElement(By.partialLinkText("Variables"));
This will give you the a element no meter what the number is.
You can try the below:
driver.findElement(By.cssSelector("a:contains('Variables')"));
If you want the word "Variables" , use the below:
String str = driver.findElement(By.cssSelector("a:contains('Variables')")).getText().split(" ")[0];
Hope this Helps....
What I understand from your question is you want to locate the <a> tag which contains text "Variables".
Try using this xpath:
//div[#class="field"]/a[contains(.,"Variables")]
This xpath will locate the <a> tag after the div tag with class name =field and Contains method with <a> tag will find the element which contains text "Variables"
try this:
String varText = driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();

Finding element with dynamic xpath not working

I have this in the HTML:
<textarea name="comment" class="form-control" rows="3" id="textarea_1160688690910416779_2159935466"></textarea>
I want to interact with id=textarea_, but the numbers are constantly changing after the "_". To solve this, I used this code:
driver.findElement(By.xpath("[starts-with(#id, 'textarea')")).sendKeys(comment);
However I am getting the error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
Use
driver.findElement(By.xpath("//textarea[starts-with(#id,'textarea_')]")).sendKeys(comment);
The reason you're getting this error is that you need to add '//' before your current XPath.
You could either use starts-with, this way:
("//textarea[starts-with(#id, 'textarea_')]")
You could also give 'contains' a try:
("//textarea[contains(#id, 'textarea_')]")
In both ways, you could use //*[... instead of textarea, for more cases
Your Xpath is not a valid Xpath.
It should contain a path and tag name
and it should be balanced (your [ is not closed)
So, if your tag is, textarea, and if it is at top: use /textarea
if not at top, use //textarea
This gives: "//textarea[starts-with(#id, 'textarea')]"
To use with selenium, you could also read that: JAVA - How to use xpath in selenium
and that: Webdriver findElements By xpath

Java - jsoup get element with specific string

I would like to select an element that matches a specific String
<img src='http://iblink.ch/resized/sjg63ngi3h3g4a.jpg' alt='tree'>
since I don't have a specific class or div to trigger I try to use getElementsContainingOwnText("resized")
method to get this element.
But it does not find it?
I also try: getElementsContainingText
Same output :(
Anyone have any idea?
The text is the part outside the tags: <tag attribute="value">Text</tag>
So you want to select Elements with a certain attribute value like this:
Elements els = doc.select("img[src*=resized]");
Have a look into CSS selectors as they are implemented in Jsoup.

I am trying to click on link using java webdriver which is generating runtime with some number at the end

I am trying to click on link which is generating runtime with some number at the end :
Html code is as below:-
<span class="float-left">
<a href="/Key/Lock/369">
How to use regular expression for xpath? please help.
It is working for exact no like:-
WebElement LockAndEdit = driver.findElement(By.xpath("//*[#href='/key/Lock/370']"));
I am trying below code with regular expression at last for any number but not working:-
WebElement LockAndEdit = driver.findElement(By.xpath("//*[#href='/key/Lock/\\d+']"));
Please help.
You don't need to go that far.
An XPath expression like this will work fine:
//a[starts-with(#href, '/Key/Lock')]
And even a normal CSS selector:
a[href^='/Key/Lock']
Note, this assumes it is always a number at the end, if it may not be, then it becomes much more difficult.

Categories

Resources