I need to find a specific href link below is an example of 3 rows. The rows are very similar but they are a bit different. I need the link to the Product ABC that is MSSQL and CS
<tr>
<th class=\"align-middle\" scope=\"row\">
<span class=\"badge bg-primary position-relative py-2\">Product ABC
<span class=\"position-absolute top-0 start-100 translate-middle badge rounded-pill bg-secondary\">P3
</span>
</span>
</th>
<td class=\"align-middle small\">MySQL</td>
<td class=\"align-middle small\">MR</td>
<td class=\"align-middle small\">
<div class=\"btn-group\" role=\"group\">
<span data-bs-placement=\"left\" data-bs-toggle=\"tooltip\" title=\"\" data-bs-original-title=\"Show Application\" aria-label=\"Show Application\">
<a class=\"btn btn-sm btn-outline-primary\" href=\"/repo/applications/328\">
<svg class=\"bi flex-shrink-0\" height=\"18\" role=\"img\" width=\"18\">
<use href=\"#icon_eye\"></use>
</svg>
</a>
</span>
</div>
</td>
</tr>
<tr>
<th class=\"align-middle\" scope=\"row\">
<span class=\"badge bg-primary position-relative py-2\">Product ABC
<span class=\"position-absolute top-0 start-100 translate-middle badge rounded-pill bg-secondary\">P3
</span>
</span>
</th>
<td class=\"align-middle small\">MySQL</td>
<td class=\"align-middle small\">MR</td>
<td class=\"align-middle small\">
<div class=\"btn-group\" role=\"group\">
<span data-bs-placement=\"left\" data-bs-toggle=\"tooltip\" title=\"\" data-bs-original-title=\"Show Application\" aria-label=\"Show Application\">
<a class=\"btn btn-sm btn-outline-primary\" href=\"/repo/applications/329\">
<svg class=\"bi flex-shrink-0\" height=\"18\" role=\"img\" width=\"18\">
<use href=\"#icon_eye\"></use>
</svg>
</a>
</span>
</div>
</td>
</tr>
<tr>
<th class=\"align-middle\" scope=\"row\">
<span class=\"badge bg-primary position-relative py-2\">Product ABC
<span class=\"position-absolute top-0 start-100 translate-middle badge rounded-pill bg-secondary\">P3
</span>
</span>
</th>
<td class=\"align-middle small\">SQLServer</td>
<td class=\"align-middle small\">CS</td>
<td class=\"align-middle small\">
<div class=\"btn-group\" role=\"group\">
<span data-bs-placement=\"left\" data-bs-toggle=\"tooltip\" title=\"\" data-bs-original-title=\"Show Application\" aria-label=\"Show Application\">
<a class=\"btn btn-sm btn-outline-primary\" href=\"/repo/applications/330\">
<svg class=\"bi flex-shrink-0\" height=\"18\" role=\"img\" width=\"18\">
<use href=\"#icon_eye\"></use>
</svg>
</a>
</span>
</div>
</td>
</tr>
I currently have this
element = driver.find_element(By.XPATH, "//tr[.//span[contains(.,'Product ABC')]]//a")
element.get_attribute("href")
The code above works but is returns the first Product ABC that it sees in some cases that is ok but some times its incorrect. How do i make sure i filter my xpath so I return the href applications/330 and not the others.
CodePudding user response:
In case you want to select the a
element containing the desired href
link based both on Product ABC
value and on SQLServer
value the XPath locator will be as following:
element = driver.find_element(By.XPATH, "//tr[.//span[contains(.,'Product ABC')] and .//td[contains(.,'SQLServer')]]//a")
In case you will need to add dependency of CS
too, it can be added in the same way here:
element = driver.find_element(By.XPATH, "//tr[.//span[contains(.,'Product ABC')] and .//td[contains(.,'SQLServer')] and .//td[contains(.,'CS')]]//a")
In case you will need to locate the link containing element based on MySQL
or/and on MR
this can be done in the same manner.