Anyone has experience with get nodes in svg by xpath?

4,148 views
Skip to first unread message

godsarmy

unread,
Aug 30, 2011, 4:50:54 AM8/30/11
to Selenium Users
Hi

I saw a number of thread talking about dealing with svg by xpath in
Selenium in this group but there seems no clear result yet.

I have a html page having code as following and i want selenium to do
a click action on the rect node.

<div id="div_root">
<svg id="random-1">
<rect id="random-2" />
</svg>
</div>

Since svg and rect's id are randomly generated by javascripts, i have
to locate the rect node from div. However, apparently, xpath
expression such as //div[@id='div_root']/svg[1]/rect[1] does not work
according to past discussion.

Then i found i could get the svg lists by //*[name()='svg'], but there
is still no way to get rect node in svg lists.

Could anyone shed some light on me?

santoshsarma jayanthi

unread,
Aug 30, 2011, 5:09:12 AM8/30/11
to seleniu...@googlegroups.com
try this

selenim.getAttribut("//divv[@id='div_root']/child::svg[contains(@id,'random')]/child::rech@id");

this will give you id of rect tag


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--
Regards,
-SantoshSarma

santoshsarma jayanthi

unread,
Aug 30, 2011, 5:09:39 AM8/30/11
to seleniu...@googlegroups.com
try this

selenim.getAttribute("//divv[@id='div_root']/child::svg[contains(@id,'random')]/child::rech@id");

this will give you id of rect tag
On Tue, Aug 30, 2011 at 2:20 PM, godsarmy <godsa...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--
Regards,
-SantoshSarma

godsarmy

unread,
Aug 30, 2011, 9:05:49 AM8/30/11
to Selenium Users
Really appreciate your help.

Although i have no idea why your suggestion does not work in my
environment, i eventually figure out a way to operate rect node.

my expression is "//*[name()='svg' and contains(@id, 'random')]/
*[name()='rect' and contains(@id, 'random')]"

Thanks anyway.

On Aug 30, 5:09 pm, santoshsarma jayanthi <santoshsarma...@gmail.com>
wrote:
> *Regards,*
> *-SantoshSarma*

Makarand Adnaik

unread,
Nov 26, 2014, 6:03:41 AM11/26/14
to seleniu...@googlegroups.com
Hi Santosh,

This good solution which is really worked for me too, but how would i go to element further under svg following is my eg.

<svg version="1.1" style="font-family: "Lucida Grande","Lucida Sans Unicode",Arial,Helvetica,sans-serif; font-size: 12px; background-color: transparent;" xmlns="http://www.w3.org/2000/svg" width="493" height="303">
<desc>Created with Highcharts 4.0.3</desc>
<defs>
<rect class=" highcharts-background" x="0" y="0" width="493" height="303" strokeWidth="0" fill="#FFFFFF"/>
<g class="highcharts-series-group" zIndex="3">
<g class="highcharts-data-labels highcharts-tracker" visibility="visible" zIndex="6" transform="translate(10,30) scale(1 1)" opacity="1" style="">
<g class="highcharts-legend" zIndex="7" transform="translate(72,269)">
<g class="highcharts-tooltip" zIndex="8" style="cursor:default;padding:0;white-space:nowrap;background-color:#ffffff;border-color:#e5e5e5;border-radius:0;border-width:1;" transform="translate(173,143)" opacity="0"> 

till SVG and version i have reached like//*[name()='svg' and contains(@version, '1.1')] but to go further on the part <g class="highcharts-legend", i dont know how to proceed, does anyone has solution for this?
Reply all
Reply to author
Forward
0 new messages