Selenium Webdriver: is it a professional practice to use xpath?
up vote
2
down vote
favorite
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?
java selenium
add a comment |
up vote
2
down vote
favorite
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?
java selenium
From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25
Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46
Of course they can but it helps massively if your elements haveid
orclass
properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example,div[id="parent"] > div > p > h2
to traverse down through the DOM elements to the selector you want using CSS selectors.
– AJC24
Nov 10 at 23:24
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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?
java selenium
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?
java selenium
java selenium
asked Nov 10 at 22:21
pasignature
104
104
From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25
Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46
Of course they can but it helps massively if your elements haveid
orclass
properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example,div[id="parent"] > div > p > h2
to traverse down through the DOM elements to the selector you want using CSS selectors.
– AJC24
Nov 10 at 23:24
add a comment |
From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25
Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46
Of course they can but it helps massively if your elements haveid
orclass
properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example,div[id="parent"] > div > p > h2
to traverse down through the DOM elements to the selector you want using CSS selectors.
– AJC24
Nov 10 at 23:24
From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25
From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25
Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46
Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46
Of course they can but it helps massively if your elements have
id
or class
properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2
to traverse down through the DOM elements to the selector you want using CSS selectors.– AJC24
Nov 10 at 23:24
Of course they can but it helps massively if your elements have
id
or class
properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2
to traverse down through the DOM elements to the selector you want using CSS selectors.– AJC24
Nov 10 at 23:24
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
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.
add a comment |
up vote
2
down vote
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.
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
1
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
up vote
3
down vote
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.
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.
edited Nov 11 at 16:57
Navarasu
1,7981721
1,7981721
answered Nov 10 at 22:30
Damien-Amen
1,38762143
1,38762143
add a comment |
add a comment |
up vote
2
down vote
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.
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
1
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
add a comment |
up vote
2
down vote
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.
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
1
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
add a comment |
up vote
2
down vote
up vote
2
down vote
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.
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.
edited Nov 11 at 19:52
answered Nov 10 at 23:49
Navarasu
1,7981721
1,7981721
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
1
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
add a comment |
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
1
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
"[...] find selector which is [...] never changing" - dream on! :)
– Andrejs
Nov 11 at 19:16
1
1
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
Ha ha ! Updated the sentence
– Navarasu
Nov 11 at 19:53
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244005%2fselenium-webdriver-is-it-a-professional-practice-to-use-xpath%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25
Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46
Of course they can but it helps massively if your elements have
id
orclass
properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example,div[id="parent"] > div > p > h2
to traverse down through the DOM elements to the selector you want using CSS selectors.– AJC24
Nov 10 at 23:24