If you have an element in a page as follows (there are multiple select elements)
<select size="1" name="j_id0:j_id2:j_id37:j_id38:0:j_id41">
<select size="1" name="j_id0:j_id2:j_id37:j_id38:1:j_id41">
With the only identifier being its name but the name will change dynamically how would you locate it in selenium(java) without referencing the name?
I am currently using the xpath as follows
/html/body/div/div[2]/table/tbody/tr/td[2]/form/div[3]/div/div/div/div[2]/div[1]/span[3]/select
Problem being if anything on the page changes this xpath will be break.
Are there any better alternative ways which are less easily broken?
You've correctly noted that the presented XPath is quite fragile. It would be also useful to see the complete HTML of the page, or, at least, preceding and following elements of the select element. From what we have now, you can rely on the part of the name attribute:
//select[contains(#name, "j_id")]
I suppose not all ids on your site are dynamic.
So there is definitely a cleaner way to write your locator.
Provide some more of your html structure and I can give you more details.
Give a try.. These select tags must have some labels associated. Use following-siblings and ancestor xpath axis to locate your element relative to the labels.
http://www.w3schools.com/xpath/xpath_axes.asp
Related
Here is the button I am attempting to click on
Log out
Here is what I have tried
driver.findElement(By.xpath("//a[contains(#class,'menu-linkRow')]")).click();
driver.findElement(By.xpath("//a[#href='/logout/?t=1550846736%2C09865a11c32ef819fb524c408c8f36cc']")).click();
You can try,
driver.findElement(by.linkText("Log out")).click();
It would be clear if give more details, like the exception you are getting and more!
Cheers!
Usually locating elements by xpath is a bad idea.
Try other approaches such as:
Locating by CSS Selector (should be your FIRST approach everytime) (This little guide will help you understand them). This includes the ability to specify patterns on element attributes such as:
[attribute~=value] [title~=flower] Selects all elements with a title attribute containing the word "flower"
Locating by any other strategy EXCEPT xpath
Locating by xpath as the very last resort.
Locating by xpath is considered an expensive operation and is extremely difficult to mantain.
You can also use whatever strategy you like but getting a collection of elements and later filtering them out by means of your favourite programming technique (i.e using Java8 Streams api), o just running another element search inside your elements such as:
element.findBy...
I strongly recommend adopting css selectors, as they are being heavily used to add style to any modern web application. So if the developer managed to resolve styling with css selectors, you will also be able to.
driver.findElement(by.Css('a.menu-linkRow')).click();
Also your second sentence "//a[#href='/logout/?t=1550846736%2C09865a11c32ef819fb524c408c8f36cc']" is using a session based locator which will not work on another session.
There is not need to use text and xpath as will be slower than css.
I am testing a web app using selenium and java. I've always avoided xpath like it was a disease. Unfortunately, I got stuck on a stubborn web element buried deep inside a table unfortunately with no id or class. I tried everything and even invited my great great grand parents but nay...nothing worked, except xpath...see below.
I tried: className, name, cssSelector e.t.c. with e.g.
driver.findElement(By.className("kujes")).click();
This is what worked.
driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();
I do not want anything less than professional in my work.
So, my questions are is xpath reliable and a good practice?
Is it professional to use xpath?
driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();
The above approach is very very bad practice.
Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.
It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.
First you can try with id or class name which are unique.
Then we can play with css selector to find,
Element with attribute, classname , id and combination.
Element which is child of another element,
Element which is next sibling of another element.
You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.
driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()
You can use relative xpath
driver.findElement(By.xpath("//table[#id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()
There are few cases which are possible only with XPath in selenium
Finding parent element of an element
Finding preceding sibling of an element
Finding an element with innerText
Finding nth element of the locator
The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.
I am currently working on the Robot Framework and using Selenium2Libraries to work on a Web Application. I'm working on a Form and I'm dealing with a dynamic elements which is an editable text area and drop down list..
I really hope someone would be able to guide me on how I can do this. An example of what I am doing is,
[Example element code]
input id="textfield-1237-inputEl" class="x-form-field x-form-text x-form-text-default x-form-focus x-field-form-focus x-field-default-form-focus"
data-ref="inputEl" size="1" name="textfield-1237-inputEl"
maxlength="200" role="textbox" aria-hidden="false" aria-disabled="false"
aria-readonly="false" aria-invalid="false" aria-required="false" autocomplete="off" data-componentid="textfield-1237" type="text"
Any information on this would be much appreciated. Thanks!
There are many types of Identifiers are available.you can search,If the values are dynamic you can use Xpath Identifier to find the locator.Id can be used only for the static values.
In the above case you can use Xpath as
xpath=.//*[contains(type(),'text')]
because text is static.It wont be change.
When trying to handle dynamic IDs, and elements which dont have easy UIDs about them, the best way to go around this is using Xpath.
Xpaths are basically the location of the element within the HTML. This is kind of the best way to get around the problem of not having ID readily available (My work has no IDs anywhere I can use, thus I have no choice but to use Xpaths)
Xpaths are really powerful, if used correctly. If not they are really brittle and can be a nightmare to maintain. Ill give you an example of potential Xpaths you may have to use:
Select From List By Label xpath=(//select)[2] DropDownItem1
You said that you have a drop down. Here is a potenital "look-a-like" you would see. The Xpath here is basically saying, find the 2nd drop down you find, anywhere on the entire HTML page.
Xpaths will take a while to get your head round, esspecially if you have had the luxurary of using IDs. The tools I use in order to locate and debug Xpaths are:
FireBug
Selenium IDE
I mainly use Selenium IDE now, as it is a nice tool which basically lets you "Select" an element within the HTML and it will spurt out its ID, CSS Path, Xpath, DOM, etc... Not only that, when you come to discover more complex Xpaths, there is a "Find" tools which shows you visually, where your Xpath is pointing to (or isnt, if its wrong)
Something which really helped me was This. It is really usful and has a lot of examples for you to work against.
If you have any problems, just reply and ill try to help
More Examples:
Click Element //span[contains(text(), 'Submit')]
Input Text xpath=(//textarea)[3] Some Random Text!
As with the other answers, I propose that you use Xpath.
Using Xpath can point you to the element by identifying the relationship of that element with the other elements around it. So my suggestion is to find a static element that you could use as your starting point.
For example:
starting point has static id:
xpath=//td[#id='startingPoint']/following-sibling::select[1]
starting point has no id but has static text (usually the label of the field):
xpath=//td[contains(text(),'Field Label')]/following-sibling::select[1]
If you could give us an idea of what the element is..we could provide you better examples..
What I did was alter the Xpath for example:
//*[#id="cec9efb093e14182b361766c26fd1919"]/section/div[1]/ticket/div/div/input
And took out the Id what was being generated dynamically cec9efb093e14182b361766c26fd1919 to switch for an autoId I set to the parent element where the Id was being generated. It's a cheap fix but it works if only one of the parent element is being generated.
So the parent element has the attribute autoid=container added to it and I referenced it as [#autoid="container"]/section/div[1]/ticket/div/div/input in the robot code
I would like to know how to identify via webdriver the following html "node":
thank you <em>very much indeed</em> - Angielsko-Polski Słownik <b>...</b>
(It's just any link of google when one launch a google search)
I have googled it, however I have found only cases where the id or the class were provided.
What about in this case?
This is my failing try:
webdriver.findElement(By.xpath("//a[#href='http://www.google.pl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCcQFjAA&url=http%3A%2F%2Fpl.bab.la%2Fslownik%2Fangielski-polski%2Fthank-you-very-much-indeed&ei=Sia8U6LPCevB7AagwoCICg&usg=AFQjCNF6y7swYrp3axD0hNrCWfjovhcVPw&bvm=bv.70138588,d.bGE']")).click();
Thanks in advance.
There are several possibilities:
By.tagName("a")
However, chances are there are more than one a tag, and so the above will pick the first one it encounters. To get more specific, you can use:
By.xpath("//a[0]")
0 in this case refers explicitly to the first a tag. However, to give a precise XPath answer, I would need to see more your page code, as well as your exact requirements. You can also use:
By.partialLinkText("thank you very much indeed")
This works best if you have unique enclosed text.
You may also want to read through the rest of the locators in the API.
If you are not able to identify the the link directly, you can try based on other element.
When you have any adjacent div or element having unique value, you can refer the link relative to that.
WebElemenet element = driver.findElement(By.cssSelector("div#id a"));
This will get the link element which is present in the div having an id value of "id".
I have two columns in a spreadsheet.
One column is an XPath expression used to get a value from an existing XML document.
The other column is an XPath expression from which I need to create my XSLT/output XML document. The value grabbed from the first column will be the value placed in the second column's element.
So for example if the second column has the XPath /A/B/C, I would create
<A>
<B>
<C><xsl:value-of select = "corresponding value from 1st column"/></C>
</B>
</A>
If the next XPath is /A/B/D, I would add
<D><xsl:value-of select = "corresponding value from 1st column"/></D>
as a sibling of C.
I'm expected to create this output XML/XSLT structure by hand. However there are thousands of lines.
I'm looking for suggestions on how to do this programmatically in Java. I've never mixed Java/XML/XPath so maybe there are libraries that can help with this?
If it's an enormous undertaking I won't be able to justify it as opposed to just doing it by hand. If I can write something that gets me most of the way there I'd be happy.
Is this a pipe dream?
Sure, most Java XML libraries support retrieving DOM nodes via XPath quite painlessly. Often they use Jaxen as their backend, so make sure you have the Jaxen JAR in your class path. See XPath support in JDOM, DOM4J, and XOM.