| || |
- 1 Post By sathish804
- 1 Post By dlai
best Xpath for grabbing only specific nodes from <ul> tag in a website
What should be a recomended Xpath expression for selecting all nodes <li class = "item - '' item-video> form <ul>. I marked this nodes with red arrows. I need to grab them in one expression and only them.
This xpath will work.
Thanks and Regards,
Personally I'd avoid xpath when ever possible, they tend to be more brittle. CSS are more stable because those are written with a UI/UX developer to consume in mind.
Originally Posted by vertigo448
Xpath.. you can do, (I'm assuming you specifically want the ones under #boxEconomy / article / boxContent
//*[id@='boxEconomy']//article/section[@data-section='economy']//div[contains(@class, 'boxContent')]//li[contains(@class, 'item-video')]
The reason why I'm using contains() instead of just '=' on the 'class' attributes, is because those could in theory be altered during runtime as styles are applied or removed.
The CSS way of doing that same xpath is..
#boxEconomy article section[data-section='economy'] li.item-video
It's a little bit cleaner and more concise.
Thanks guys very much for help. @dlai yea the CSS is more cleaner and for what I learn CSS syntax is a little nicer for me to understand, but in my company everybody is pushing more towards Xpath, I'm new to webdriver, frankly I am new to automatition nad I have yet lots of things to learn.
Again thx for advise.
even I prefer css or directly go with ID, Name when using selenium
worth taking a look at - https://docs.google.com/document/d/1...it?usp=sharing
You should be able to grab CSS basics and xpath which I collected sometime back.
Avoid using Xpath, if the layout of the page changes, it will likely break your tests.
Id should always be the first choice where possible as it is less likely to be changed.
In large organizations, generally I prefer CSS than IDs.. In large enterprises that have multiple teams, CSS is like a contract between the web team and the UI/UX team. The developer provide style classes that UI developers could bind to.
ID's do have their advantages. They tend to be shallower selectors since you don't deal with parents. And they also are faster as they are indexed by the browser.
CSS has its downside too. If the developers do not have a clear separation of functionality and style, it'll suffer some of the same problems. For example, this is generally common in users of BootStrap styling framework. People decorate with things like .button-primary, .button-default, etc... instead of describing their feature/function like ".login-confirmation-button". This causes the problem of a lot of noise and brittle CSS.