I have this very complicated xpath:
/html/body/div/div/div[2]/div/div/div/div[3]/div/table[1]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr[1]/td/div/div/table/tbody[1]/tr[3]/td[2]/div/table/tbody/tr/td/table/tbody/tr/td[1]/span/input
The span has this class="z-combobox-designation z-combobox".
I need to access the span element using its class and not through xpath. Is it possible with cssSelector? Can someone give me a hint how to do it?
Yes you can use css selector in this way to select your element based on compound classes
driver.findElement(By.cssSelector(".z-combobox-designation.z-combobox"));
or specify the tag name with classes like span.z-combobox-designation.z-combobox
If you have same kind of element more then one in your HTML then consider the unique parent of it and locate the same
E.g. there is parent table of your element which have unique id e.g. so you can consider that table tag fist and locate span based on that table e.g.
table#someid span.z-combobox-designation.z-combobox
Related
I want to access the 2nd element with same class name using css selectors.
1st element:
<a class="good">
2nd element:
<a class="good">
Css selector I am using :
a.good
but this accessing both of them.
How to access the 2nd one or anyone individually?
you can use pseudo selector, pseudo selector matches elements based on their position among a group of siblings.
check example link below
click here
I am trying to get the xpath of a svg that has an attribute <use href= "#icon-map">
So far the path //*[local-name()='svg']/*[local-name()='use'] works, but it finds 84 entries.
How can I modify the xpath in order to select only the use that has the href as "#icon-map"?
You can use this:
//*[local-name()='svg'][use[#href="#icon-map"]]
or
//*[local-name()='svg'][*[local-name()='use'][#href="#icon-map"]]
See example.
If you have more results than you expect then you should use more specific paths to the element or take your query into (..) and add number of an item into [..] like :
(//*[local-name()='svg'][use[#href="#icon-map"]])[2]
If use is an attribute then you could do this :
//*[name()='svg']//*[#use and #href='#icon-map']
Also the above solution assumes that #icon-map is unique in HTML DOM
I am wondering if below is achievable using xpath
Given:
<label for="pt1:sc">Select Country</label>
<select id="pt1:sc">....</select>
Requirement:
I want to find select element using single xpath expression like below,
bcs ids are dynamic and always available in attribute 'for'.
//label[text()='Select Country']/#for//*[#id=#for]
Can we pass attribute value(here for attribute of label) in xpath, further down the path to find element.
Please do not suggest alternative using siblings, child, id or selenium get-attribute etc.
Thanks,
You can use something like this to select an element with an attribute value which refers to another attribute located in another element :
//*[#id=//label[text()='Select Country']/#for]
I'm not sure how it's going to work with your actual html, but it works on the example in the question:
//label[text()='Select Country'][#for=//select/#id]
So let's say my html structure have couple of similar div elements.
body/div/...
body/div/...
body/div/...
body/DIV/div[#class='class']
I would like to access the last one. But the one that is upper-cased.
So "//body/div/" selector will find many, but not related elements.
And "//div/div[#class='class']" will select the child div, not the upper case parent.
Use parent to select the parent of a given element:
//div/div[#class='class']/parent
If you want to be more verbose, you can also select a parent by a given tag name:
//div/div[#class='class']/parent::div
I have this code for xpath and html:
<a class="WatchButton inicon" rel="nofollow" data-productid="111124">
xpath=/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a
How can I get the data-productid value?
Just add #data-productid to the xpath expression:
/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a/#data-productid
Note that the xpath expression you have is very fragile since it depends on a bunch of elements and their relevant positions. Try to rely on the element's attributes or one of it's containers - look for id and class attributes. For example:
//a[contains(#class, "WatchButton")]/#data-productid
This gets the first link anywhere on a page that contains WatchButton class and retrieves it's data-productid attribute value.
* Sharing the link to the web page or showing the complete HTML could help to provide you with a more reliable xpath expression.