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 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 style="width:90px;"> </td>
<td style="width:40px;">3</td>
<td >4</td>
<td >18200854</td>
<td align="right" valign="middle" style="width:60px;">
<a id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl02_HyperLinkFCEC" 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" 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 >40448965</td>
<td align="left" valign="middle" style="width:75%;">
<span id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl03_lblDocDesc">Document</span>
</td>
<td style="width:90px;"> </td>
<td style="width:40px;">27</td><td >4</td>
<td >18200855</td>
<td align="right" valign="middle" style="width:60px;">
<a id="ctl00_ContentPlaceHolder1_gridViewEvents_ctl15_gridViewEventDocs_ctl03_HyperLinkFCEC" 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" 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!
CodePudding user response:
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