Skip to content
Advertisement

Get Text from SVG using Python Selenium

My first time trying to extract data from an SVG element, following is the SVG element and the code I have tried to put up by reading stuff on the internet, I have absolutely no clue how wrong I am and why so.

<svg class="rv-xy-plot__inner" width="282" height="348">
  <g class="rv-xy-plot__series rv-xy-plot__series--bar " transform="rrr">
    <rect y="rrr" height="rrr" x="0" width="rrr" style="rrr;"></rect>
    <rect y="rrr" height="rrr" x="0" width="rrr" style="rrr;"></rect>
  </g>
  <g class="rv-xy-plot__series rv-xy-plot__series--bar " transform="rrr">
    <rect y="rrr" height="rrr" x="rrr" width="rrr" style="rrr;"></rect>
    <rect y="rrr" height="rrr" x="rrr" width="rrr" style="rrr;"></rect>
  </g>
  <g class="rv-xy-plot__series rv-xy-plot__series--label typography-body-medium-xs text-primary" transform="rrr">
    <text dominant-baseline="rrr" class="rv-xy-plot__series--label-text">Category 1</text>
    <text dominant-baseline="rrr" class="rv-xy-plot__series--label-text">Category 2</text>
  </g>
  <g class="rv-xy-plot__series rv-xy-plot__series--label typography-body-medium-xs text-primary" transform="rrr">
    <text dominant-baseline="rrr" class="rv-xy-plot__series--label-text">44.83%</text>
    <text dominant-baseline="rrr" class="rv-xy-plot__series--label-text">0.00%</text>
  </g>
</svg>

I am trying to get the Categories and corresponding Percentages from the last 2 blocks of the SVG, I’ve replaced all the values with the string ‘rrr’ just to make it more readable here.

I’m trying,

driver.find_element(By.XPATH,"//*[local-name()='svg' and @class='rv-xy-plot__inner']//*[local-name()='g' and @class='rv-xy-plot__series rv-xy-plot__series--label typography-body-medium-xs text-primary']//*[name()='text']").get_attribute('innerText')

Like I said, I don’t know what I’m doing here, what I’ve so far understood is svg elements need to be represented as a ‘custom ?’ XPATH which involves stacking all elements into an XPATH which is relative to each other, however I have no clue on how to extract the expected output like below.

Category 1 - 44.83%
Category 2 - 0.00%

Any help is appreciated. Thanks.

Advertisement

Answer

You can try something like :

for sv in driver.find_elements(By.XPATH,"//*[local-name()='svg' and @class='rv-xy-plot__inner']//*[local-name()='g' and @class='rv-xy-plot__series rv-xy-plot__series--label typography-body-medium-xs text-primary']"):
     txt= sv.find_emlement(By.XPATH, './/text').text
     print(txt)

#OR

   for sv in driver.find_elements(By.XPATH,"//*[local-name()='svg' and @class='rv-xy-plot__inner']//*[local-name()='g' and @class='rv-xy-plot__series rv-xy-plot__series--label typography-body-medium-xs text-primary']//text"):
        txt= sv.text
        print(txt)
         
         
Advertisement