Selenium XPath: how to get href value of a use attribute - selenium

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

Related

Find xpath of element knowing a part of href attribute only selenium ide ui vision katalon recorder

I need to detect via xpath an element (image with href) but i know a part of href only
This is the html
<img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60">
I know the id 123456 (part of href)
I tried this xpath that recognize element with a part of href but working in text link only
xpath=//a[contains(#href, "123456")]
How can i detect the element using a part of href only ?
I need xpath only please.
You need get /img in //a where href attribute contains() or ends-with() your id
This is XPATH that you need. At least i would use this XPATH in this situation
//a[ends-with(#href, 'your-id-here')]/img
You can use regular expresions. Something like
driver.find_element_by_xpath("//input[starts-with (#name,'Tut')]")
or as you described
driver.find_element_by_xpath("//input[contains(#name,'sel')]").
Be awere of one thing, do not use double quotes, for the string you are searching as atribute value. Use a single quote like I previously described.

XPath : Pass attribute value down the path

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]

Is it okay to use such xpath to find web elements?

Consider this xpath which should always return one element.
//div[#id='MyDiv123']/div[contains(#class, 'super')]
Assume that we won't add anymore divs whose class is super. Given that info, I don't think that it is a good idea to use /div[contains(#class, 'super')]because the xpath will break if div[contains(#class, 'super')] is placed inside another element.
Shouldn't we be using //div[contains(#class, 'super')] instead ?
I don't like using XPaths for locators that can be written as a CSS selector. I think it's much simpler as
#MyDiv123 > div.super
or just
div.super
if it's unique on the page.
XPath contains() is a string match. All the elements below will match your XPath locator but none of them will match the CSS selectors above.
<div class="super-duper" ...>
<div class="superior" ...>
<div class="abcsuperdef" ...>
... you get the idea...
There is no defined Best Practices while writing xpaths. It all boils down to how effective xpath can be written.
I don't see any issue with the xpath as :
//div[#id='MyDiv123']/div[contains(#class, 'super')]
Of-coarse there ca be some improvements as follows :
As an enduser you won't be sure how the class attribute super impacts the HTML or which elements have this attribute. So in that case to identify the WebElement uniquely it would be wise to include the ancestor <div> tag with id as MyDiv123.
But it doesn't looks like the classname super can be dynamic. Hence you can avoid the keyword contains within the xpath and rewrite it as :
//div[#id='MyDiv123']/div[#class='super']

Selenium/java-xpath for xlink:href attribute

I tried to locate use element with attribute value '#aaa' for below snippet
<svg>
<use xlink:href='#aaa'></use>
</svg>
I was able to locate using css selector use[* |href='#aaa'] . But I require it in xpath. I tried below xpath but it is not working
(//*[name()='use'])[ * ='#aaa' or #href='#aaa']
Note :I need to use the value '#aaa' to differentiate from other elements.
Could anyone please help to construct this xpath.
Your predicate [ * ='#aaa' or #href='#aaa'] returns false as there is no attribute as href (but xlink:href), and use has no child nodes with text content '#aaa'.
Note that wild-card for any attribute is #* while just * is for any node (except attribute). Try below XPath
"//*[name()='use' and #*='#aaa']"
lookout, if someone needs contains..
"//*[name()='use'][contains(#*='#aaa')]"
as none of the suggestions really worked for me, I came up with following (tested in chrome devtools):
//*[name()='use' and #*[contains(.,'#aaa)]]
comments:
name()='use'
because a normal tag search (i.e //use) did not work
#*[contains(.,'#aaa')]
because contains with a wildcard (i.e. contains(#*,'#aaa')) didn't work
Use the xpath
//use[#xlink:href='#aaa']
or
//use[contains(#xlink:href,'#aaa')]
As the element is within svg namespace you can use the following xpath:
//*[name()="svg"]/use[contains(#xlink:href,'#aaa')]
This is an SVG tag and it works a little differently
Please try below xpath:
//[local-name()='svg']/yourxpath
For XPath:
//*[name()='use'][contains(#*, '#aaa')]
Or variant provided by Andersson
Approaches with [#xlink:href='#aaa'] don't work on practice

How to identify the element name in selenium

The element to match is as follows:
... some html....
<table>
some <tr's>
<tr><td>"caption in a <a> tag"</td><td> result value here </td></tr>
more <tr's>
.... more html
I'm using Selenium IDE but don't see a way to match/capture the result value text anywhere ?
Given
<yyy name="iggy" caption="abcd" level="20"></yyy>
you would want to use
assertAttribute("iggy#caption", "abcd")
This uses the name as a locator and #caption to specify the attribute you want. You could follow it with
assertAttribute("iggy#level", "20")
to check the level attribute. You might use 'validateAttribute' instead of 'assertAttribute'. If you want to capture the attribute value use 'storeAttribute'.