I use jQuery to get values of presaved elements from some websites, using paths like this:
HTML BODY #bodyContainer #mainContentContainer #mainContent #productContentRight #swatchContent #colorSwatchContent SPAN
The problem i faced when the websites page contains tables and there are same element in another similar path such as:
/html/body/div/center/div/div[3]/div/table/tbody/tr[5]/td/div/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[3]/td
In the last path as you can see that there are 5 tr which means that its possible to find the same element in another path.
I use the path as a selector for jQuery and jQuery will return array of elements, i don’t know which one is the right element.
So my question is:
How to save the path for better later use? and how to parse this new path to be ready as a jQuery selector.
If the question is not clear please ask me and i will do my best to explain more.
I don’t know why there are so many answers that you are using XPath because XPath was deprecated a long time ago and jQuery no longer supports it without the XPath compatibility plugin.
See Release Notes of 1.2 : http://www.learningjquery.com/2007/09/upgrading-to-jquery-12
XPath compatibility plugin : http://docs.jquery.com/Release:jQuery_1.2#XPath_Compatibility_Plugin
Just use
$("#colorSwatchContent span")as your selector. Which is a css style seclector meaning find me all descendent span elements of an element with id colorSwatchContent. Since id’s in html are unique identitfiers, this is about as specific as you can get.$("#colorSwatchContent > span")will only select DIRECT descendents (immedieate children)$("#colorSwatchContent > span:first")will select the first span direct descendent