-1

I would like seach which of these siblings has a specific text 'app'. Since there are no absolute references I start my search from the word 'DOD'. Afterwards, I search: following-sibling::td/div/table/tbody and then I would like to know if any of the following 'tr' (2 in this case) has the word 'app'. Afterwards, navigate to the element and click it.

Following is the HTML:

<tr align="center">
    <td align="left" valign="top" style="width:30%;">Application</td>
    <td align="left" valign="top" style="width:30%;">DOD 11-24-2021</td>
    <td align="right" valign="middle" style="width:32%;">
        <div>
            <table class="Nested_ChildGrid" cellspacing="0" cellpadding="0" rules="rows" border="0" id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs" style="color:Black;border-width:0px;border-style:None;width:100%;border-collapse:collapse;">
                <tbody>
                    <tr align="center">
                        <td align="center" valign="middle" style="width:70px;">
                        <td align="left" valign="middle" style="width:75%;">
                            <span id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl02_lblDocDesc">Application</span>
                        </td>
                        <td class="Hide" style="width:90px;">&nbsp;</td>
                        <td style="width:40px;">3</td>
                        <td class="Hide">4</td>
                        <td class="Hide">18200854</td>
                        <td align="right" valign="middle" style="width:60px;">
                            <a id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl02_HyperLinkFCEC" class="doclinks hidden-print" aria-label="Document ID" href="Ecomm/ViewEdocs.aspx?ID=uhJ1x784NcaNrVVzKW0vYhObQ19y7BXopEw8HPMBvPh59c8wKiz7EfP0U9mCs68+xxQ9sHISXdf9TbzLHLm9u3qat1tCpKJn7jkKCXxMf3CGdx1PMPNivnJv5+IoPv/FaerVR/7cUZCYG+WJjeFE0/4qN1D5ZZGZAl9+i1QlqiXCZyQI10yEtQc9FF2/d0mn" target="_blank">View</a>
                            <a id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl02_HyperLinkFCECLegacy" class="doclinks hidden-print" aria-label="Document ID" href="Ecomm/ViewEdocs.aspx?ID=KMjX55lU5BVhR9pieAD4vZ06nmikECALhQiRaHwCUvFJVB0oLK/aItQZWZ7AYse5NEZaq+5vZ+uM26hmC5u2Lklzx02fM/sralEBjeeLz/+QayOXHkcVDGSG8y52q5CG1CcLxXRHqpChF5pMkTPvLp6oTyQgVUXB6a2eQHUYKGz+tTboJtXPdYTift/M68Uo" target="_blank"></a>
                        </td>
                    <tr align="center">
                        <td align="center" valign="middle" style="width:70px;">
                        <td class="Hide">40448965</td>
                        <td align="left" valign="middle" style="width:75%;">
                            <span id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl03_lblDocDesc">Document</span>
                        </td>
                        <td class="Hide" style="width:90px;">&nbsp;</td>
                        <td style="width:40px;">27</td><td class="Hide">4</td>
                        <td class="Hide">18200855</td>
                        <td align="right" valign="middle" style="width:60px;">  
                              <a id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl03_HyperLinkFCEC" class="doclinks hidden-print" aria-label="Document ID" href="Ecomm/ViewEdocs.aspx?ID=uhJ1x784NcaNrVVzKW0vYslV1nQ3hIRYmXLxbQq33tyshCGyHbOIapchVlNHL/s6sP+5f245ruOERRUrUYOrpDvAZFR42jcGUshFcX0WcMX/1Ds0ACEkxbDF0eRUp5DOJADJWKC/sJ23XH2yH6W4IJrWYGJEcqPrcIUgkyRWqCByEnW0a9EhaaoRk0ocHKwK" target="_blank">View</a>
                              <a id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl03_HyperLinkFCECLegacy" class="doclinks hidden-print" aria-label="Document ID" href="Ecomm/ViewEdocs.aspx?ID=KMjX55lU5BVhR9pieAD4vZ1FGtwzRAaZfXZY4CPE4p5XSrA4rq/HgZJGXPnI2Kl2vx8vyKEZmh2chQz0Pw13X2YMFwRVg83JzCI/gTQFB1L+NKDaHm2c8Cxjs9UzF5U7u5jq9KLxemMKWMSqI+IaUv0wdeuSsaS2LMFBPnmYClZmdrCp955OJux9WvsrcJH9" target="_blank"></a>
                        </td>
                    </tr>

And here is some code I have been trying out without success:

path = driver.find_element(By.XPATH, '//td[contains(text(), "DOD")]/following-sibling::td/div/table/tbody/*/*/span[contains(text(), "app")]/parent::*/td[5]/a').click()

Thank you in advance!

VonViru
  • 23
  • 6
  • What exactly are you trying to do here? – Prophet Mar 10 '22 at 08:35
  • haha sounds complicated. The thing is that there is a dynamic amount of rows in that table for me to define a rigid XPATH with absolute reference. So what I wanted to accomplish was that, starting from a reference point (in this case 'DOD' move in the same row. The thing is that for the same row in 'DOD', there can be more than one rows where to find 'app' (sometimes there are more than 1 row in the same row). Additionally, each of these rows, has in the same 'smaller row' a href that I need to access only if the word 'app' is shown. Basically I need to click the link related to the App row. – VonViru Mar 10 '22 at 14:58
  • I am doing it in this complicated way since there are other 'App' words in the column which I don't know how to filter out. That is why I start in this relative reference being 'DOD' which always will be shown in the row I need. Thanks for your time – VonViru Mar 10 '22 at 15:01

1 Answers1

-1

To identify the <td> element with text as DOD and find the respective <span> with text as App and then click on the relevant <a> tag with text as View you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[starts-with(., 'DOD')]//following-sibling::td[.//span[starts-with(., 'App')]]//following-sibling::td[.//a[text()='View']]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you for your reply, althought it didn't work, it gave me another idea to pursue and finally made it work. – VonViru Mar 10 '22 at 19:00
  • Done, would you happen to know of any place where I could read more about working with Selenium for Web-Scrapping? Thanks. – VonViru Mar 10 '22 at 19:20
  • 1
    A great way to start would be to visit the discussions from [Selenium](https://stackoverflow.com/a/54482491/7429447)'s [Frequent](https://stackoverflow.com/questions/tagged/selenium?tab=Frequent) tab. – undetected Selenium Mar 10 '22 at 19:23