Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XPATH: Select link on table based on content of a different field on same row

Options
  • 05-07-2017 9:57am
    #1
    Registered Users Posts: 5,542 ✭✭✭


    Below fields are in table rows of a web page. I want to basically click the "Download certificate" link but only on the row that contains "15 Jun 2017". Can anyone advise an xpath that will only goto this row
    TestTraining1-5375  Test_training1.zip  15 Jun 2017
    00:00:00    
    UA-1-5375
    UA-2-5375
    Download certificate
    
    TestTraining1-5375  Test_training1.zip  16 Jun 2017
    00:00:00    
    UA-1-5375
    UA-2-5375
    Download certificate
    

    So i can get either using
    //*[contains(text(),'Download certificate')]@href

    

    but is there a way of getting the specific row. Not sure how to do the "and" operator in xpath
    Tagged:


Comments

  • Posts: 0 [Deleted User]


    (Hopefully not too late for you...)

    I tested with the following xml/html (which I think should be close enough to what you have):
    <table>
        <tr>
            <td>TestTraining1-5375  Test_training1.zip  15 Jun 2017</td>
            <td>00:00:00</td>
            <td>UA-1-5375</td>
            <td>UA-2-5375</td>
            <td>Download certificate</td>
        </tr>
        <tr>
            <td>TestTraining1-5375  Test_training1.zip  16 Jun 2017</td>
            <td>00:00:00</td>
            <td>UA-1-5375</td>
            <td>UA-2-5375</td>
            <td>Download certificate</td>
        </tr>
    </table>
    

    Then this XPath should work for you:
    //td[contains(text(), '15 Jun 2017')]/following-sibling::td[4]
    

    Should be fairly self-explanatory, bu this is what it's doing:
    • Find the td that matches your text
    • Get the 4th sibling td


  • Registered Users Posts: 5,542 ✭✭✭veryangryman


    (Hopefully not too late for you...)

    I tested with the following xml/html (which I think should be close enough to what you have):
    <table>
        <tr>
            <td>TestTraining1-5375  Test_training1.zip  15 Jun 2017</td>
            <td>00:00:00</td>
            <td>UA-1-5375</td>
            <td>UA-2-5375</td>
            <td>Download certificate</td>
        </tr>
        <tr>
            <td>TestTraining1-5375  Test_training1.zip  16 Jun 2017</td>
            <td>00:00:00</td>
            <td>UA-1-5375</td>
            <td>UA-2-5375</td>
            <td>Download certificate</td>
        </tr>
    </table>
    

    Then this XPath should work for you:
    //td[contains(text(), '15 Jun 2017')]/following-sibling::td[4]
    

    Should be fairly self-explanatory, bu this is what it's doing:
    • Find the td that matches your text
    • Get the 4th sibling td


    Thanks, it does seem to select both rows though (from my debugger). To verify this, have it look for the 16 Jun 2017 row. It returns the first


  • Posts: 0 [Deleted User]


    Thanks, it does seem to select both rows though (from my debugger). To verify this, have it look for the 16 Jun 2017 row. It returns the first

    Strange... works perfect for me (XML Tools plugin for Notepad++).

    What's your setup?


  • Registered Users Posts: 5,542 ✭✭✭veryangryman


    Was just thinking - Id say it just clicks the first href url it finds within the table. How do i select (say) the second. I can pass this into the test


  • Posts: 0 [Deleted User]


    TestTraining1-5375  Test_training1.zip  15 Jun 2017
    00:00:00    
    UA-1-5375
    UA-2-5375
    Download certificate
    
    TestTraining1-5375  Test_training1.zip  16 Jun 2017
    00:00:00    
    UA-1-5375
    UA-2-5375
    Download certificate
    

    Can you post the xml/html you're working from please?


  • Advertisement
  • Registered Users Posts: 5,542 ✭✭✭veryangryman


    Wondering about how to do that from a mac. If i just go view->page source, it does give html but doesnt have the table content on it (e.g The Download Certificate Link). How do i get this?


  • Registered Users Posts: 5,542 ✭✭✭veryangryman


    Actually try this

    https://prnt.sc/fscdi5


  • Posts: 0 [Deleted User]


    Well this is fun... ;)
    //td[contains(b/span,  '15 Jun 2017')]/following-sibling::td[last()]/a/@href
    

    Breaking it down...
    Find any td with children b/span containg text '15 Jun 2017' (there's likely a better way to do this, but I'm in a rush):
    //td[contains(b/span,  '15 Jun 2017')]
    

    Get the "last" sibling node for that td:
    /following-sibling::td[last()]
    

    Get the href attribute on the a node (you may need to update this part with the correct path to the href you need, I can't tell from the source you gave)
    /a/@href
    

    After all this, you're on your own mate... I need to write some code that I'm actually getting paid for :P


Advertisement