Selenium/java-xpath for xlink:href attribute - selenium

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

Related

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

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

Xpath finder for selenium using python -automation

I am trying to find an unique xpath for the below element, please advice if there are any better xpaths to make it for general text as I have currently given for that specific name.
<td tabindex="4" style="text-align: left;" title="name" class="">Name</td>
xpath i am using: //td[#title='name']
here if the name is changed with something else in the code, this wouldn't work, could someone help me identify unique xpath which works in general for any text. Thanks!
You can concatenate (using and / or) multiple attributes of element to find the element precisely .
By.xpath("//td[#title= 'name' and contains(text(), 'Name')]")
However we need to see more details of the code and your DOM of page to find element.
There will always be some element which will never change in the page(like name of table) using that as a relative point ,we can refer to the row of the table.
the simplest way to find the XPath of any element is to go to the developer options and select the markup of the element you want XPath of.
Right Click -> Copy -> XPath
I believe this is the simplest way. And you will also where you are doing wrong.
Screenshot attached for your reference.
I have used the general syntax - "//td[text()='{}']" and passing the name parameter when i define a method so that it won't be specific to one and others can test using the same locator with their name changed when someone else uses the testcase.
Thanks everyone for your response!

how to locate element with selenium webdriver for below html

I have an issue clicking on the below HTML:
<div id="P7d2205a39cb24114b60b80b3c14cc45b_1_26iT0C0x0" style="word-wrap:break-word;white-space:pre-wrap;font-weight:500;" class="Ab73b430b430a49ebb0a0e8a49c8d71af3"><a tabindex="1" style="cursor:pointer;" onclick="var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');return false;" onkeypress="if(event.keyCode == 13 || event.which == 13){var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');}return false;"><img border="0" src="/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=Microsoft.ReportingServices.Rendering.HtmlRenderer.RendererResources.TogglePlus.gif" alt="+"></a> 2013</div>
I have used the below script to click anchor inside a div tag. For the above html code it is not fixed only end part of id example "26iT0C0x0" is fixed. The script that I have used is:
WebElement e1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[ends-with(#id,'26iT0C0x0')]/a")));
e1.click();
You can use the 'contains' method within an xpath lookup:
driver.findElement(By.xpath("//div[contains(#id,'26iT0C0x0')]")
I would recommend you to consider CSS selector alternative as CSS working faster, than xpath.
So 'contains' in attribute in CSS stands for '*=', for example
if we want to find attribute by 'CSS' ending in this: <htmlTag A="blablaCSS" > we need do the following:
String CSSselector="htmlTag[A*=CSS]";
and you get this element searched.
So considering your example CSS selector be like:
String cssSearched="div[id*=26iT0C0x0] a";
also try to click not on link - a
but on parent div as well:
String cssSearched="div[id*=26iT0C0x0]";
driver.findElement(By.cssSelector(cssSearched));
hope this works for you.
As Mark Rwolands already mentioned: the xpath-Function 'ends-with()' isn't supported in Selenium 2.
Also, if you maybe consider to use chromeDriver in the future, I would recommend clicking the image, not the anchor, see:
https://sites.google.com/a/chromium.org/chromedriver/help/clicking-issues
edit:
Also your IDs are looking generated. I wouldn't count on them for a stable test-environment.

issue accessing a text using xpath

I'm trying to find Approvals in the following html and cant seem to get it. I've tried:
//td[*/text()='Approvals']
//td[contains(#class, 'Approvals')]
any help would be appreciated
<td class="ThemeGrayMainItem" name="cmSubMenuID4"
onmouseup="cmItemMouseUp (this,1,'cmSubMenuID4',0,32)"
onmouseout="cmItemMouseOut (this,1,'cmSubMenuID4',0,32)"
onmousedown="cmItemMouseDown (this,1,'cmSubMenuID4',0,32)"
onmouseover="cmItemMouseOverOpenSub (this,1,'cmSubMenuID4',0,32)">Approvals
</td>
You may use contains():
//td[contains(., 'Approvals')]
where . refers to the element's text.
You can also apply additional checks, for instance, on the class name:
//td[#class='ThemeGrayMainItem' and contains(., 'Approvals')]
I would use the following xpath:
//td[contains(text(),'Approvals')]
or if you wanted to be more specific:
//td[#class='ThemeGrayMainItem'][contains(text(),'Approvals')]

how to click on element using WebDriver

i need to click on dat element:
<span id="act_action9" class="" onclick="openDialog('export', event)">
text
</span>
i cant click on id,because it is dynamic. After a click i am getting window with some settings.
You should use xpath in order to click this element, so you could add an expression that unequivocally identify this element.
Could you please include more HTML code, because just with the code included is not enough to create a xpath expression.
I recommend this tutorial to start doing good xpath expressions:
http://www.w3schools.com/xpath/
See example for dynamic xpath
By.xpath("//span[#id [contains(.,'act_action')]]")
Great xpath cheatsheets: http://extract-web-data.com/5-best-xpath-cheat-sheets-and-quick-references/
You can also use xpath like following(assuming only digit is dynamic):
".//span[contains(#id, 'act_action')]"
As ID is dynamic, you can use xpath for text which is inside span.
driver.findElement(By.xpath("//span[text()='text']").click();
Or if part of ID remain common, then you can use
//span[contains(#id,'act')]