3

I need to determine the full xpath to reach the 3rd path element displayed here:

<div id="myID">
    <div>
        <svg version="1.1">
            <g>
                <g>
                <g>
                    <g>
                        <g>
                        <g>
                            <path fill="green">
                            <path fill="orange">
                            <path fill-opacity="0.2">

... plus all relevant closing tags.

I need to use this xpath in conjunction with Selenium-Webdriver. By looking at this example: Selenium WebDriver: clicking on elements within an SVG using XPath I understand I need need to use either the local-name() or name() methods to interact with the SVG element but I am not sure how to incorporate the initial nested divs and also reach through the nested g elements with no IDs or other information to work with.

Thank you in advance if you are able to help!

Community
  • 1
  • 1
strangenewstar
  • 175
  • 1
  • 2
  • 13

2 Answers2

3

You could get it by position for each level:

id('myID')/div/*[1]/*[1]/*[2]/*[1]/*[2]/*[3]

Or by tag name:

id('myID')/div/*[name()='svg']/*[name()='g']/*[name()='g']/*[name()='g']/*[name()='g']/*[name()='path'][3]

Or simply :

id('myID')//*[name()='svg']//*[name()='path' and @fill-opacity='0.2']
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • is the id('myID') a separate method? I attempted to use this as a base in Firebug and I cannot find the element. – strangenewstar Apr 21 '16 at 22:45
  • I used //*[@id='myID']//*[name()='svg']//*[name()='path' and @fill-opacity='0.2'] and it worked like a charm. Can you recommend a good place to look up xpath information like this? – strangenewstar Apr 21 '16 at 22:48
  • 1
    No, it's part of the XPath. It works in both Chrome and Firefox and it's defined in the XPath specs (https://www.w3.org/TR/xpath/). Note that it only works when it is used at the beginning of the XPath. – Florent B. Apr 21 '16 at 22:48
  • OK. My xpath is nestled deep inside other elements so that is more than likely why it did not work for me. Thanks again! – strangenewstar Apr 21 '16 at 22:50
0

If the other g elements are not full, you can use like:

driver.findElement(By.xpath(*[@id='myId']//*[version='1.1']//*[@fill-opacity='0.2']))
Taylan Derinbay
  • 128
  • 3
  • 13
  • This unfortunately did not work. The first relative xpath parameter does work but once I get the to svg element it is no longer valid. – strangenewstar Apr 21 '16 at 22:42