[Python] Problems to Locate SVG elements using selenium (Python)

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to do some web automation in Python using Selenium. In one part of the process, I need to interact (click) with an SVG element, but I can't locate it using XPATH, ID, or CLASS_NAME methods.

    Here is the relevant HTML snippet where the SVG element is located:

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="zenRoot" x="0" y="0" width="1530" height="660" onload="zenLoad();" onunload="zenUnload();" zoomAndPan="disable">

    <g xmlns="http://www.w3.org/2000/svg" id="703433042" lineId="respLine_703432982" rectId="message_703433042" onmousedown="svgPage.selectItem(evt, 'message', 'message_703433042', '703433042', null, 'respLine_703432982');">
    <rect id="message_703433042" class="MsgOutlineSelect" x="245" y="168" width="190" height="24" rx="4"/>
    <rect class="MsgTermSyncResponse" x="248" y="171" width="14" height="18" rx="2"/>
    <line class="MsgLineResponse" x1="425" y1="180" x2="263" y2="180"/>
    <circle class="MsgStartSyncResponse" cx="425" cy="180" r="6"/>
    <line class="MsgLineResponse" x1="268" y1="175" x2="263" y2="180"/>
    <line class="MsgLineResponse" x1="268" y1="185" x2="263" y2="180"/>
    <text class="MsgLabel" x="412" y="178" text-anchor="end">2025-06-12 10:30:03.427</text>
    <text class="MsgLabel" x="412" y="190" text-anchor="end">BuscaResultadoResponse</text>
    <text class="MsgLabel" x="243" y="182" text-anchor="end">[3]</text>
    </g>
    </svg>


    I've already tried locating every element in the HTML, but nothing is returned.

    The element is not inside an iframe, and I've already created a function to verify that the correct page is being accessed.

    Here is the element I'm trying to interact with (highlighted in the image):

    [​IMG]

    This is the code I'm using:

    xpath = '//*[local-name()="rect" and starts-with(@id, "message_")]'
    elemento = driver.find_elements(By.XPATH, xpath)
    print(elemento)


    Is there anything else I can try to interact with this element?

    If you need more information to better understand the issue, feel free to ask!

    Continue reading...

Compartilhe esta Página