1

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.

3
  • How did you get this SVG, and why are you using Selenium? If you just need to parse an SVG, you can use the xml.etree module or even BeautifulSoup to parse it. Selenium runs an entire copy of the Chrome browser. Commented Dec 3, 2022 at 3:05
  • Hi Tim, multiple reasons, I need to log on to the website via 2 factor authentication first, the SVG is Javascript dependent so I need to actually interact with the webpage, clicks and stuff. I just copied the SVG from the source page to answer your first question. Commented Dec 3, 2022 at 3:11
  • Can you post the URL or full HTML source? Commented Dec 3, 2022 at 3:53

2 Answers 2

1

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)
         
         
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Fazlul this gave me an idea finally and with some slight modification I got it working.
0
sv = 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']//*[name()='text']")

This gives me a list I can iterate through to get the values.

Thanks to the idea from @Fazlul, the modification I've made is //*[name()='text'] at the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.