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 width="282" height="348">
<g 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 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 transform="rrr">
<text dominant-baseline="rrr" >Category 1</text>
<text dominant-baseline="rrr" >Category 2</text>
</g>
<g transform="rrr">
<text dominant-baseline="rrr" >44.83%</text>
<text dominant-baseline="rrr" >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.
CodePudding user response:
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)
CodePudding user response:
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.