The below html consist of 1 of many rows. Below is a sample row I need to click on the href. The href link is dependent on DataScience td tag and the Finance td tag they are fixed tags they dont change. The tag that I am looping that changes is SomeCategory each category will have a different number instead of the 328 for this particular link.
<tr>
<th class=\"align-middle\" scope=\"row\">
<span class=\"badge bg-primary position-relative py-2\">SomeCategory
<span class=\"position-absolute top-0 start-100 translate-middle badge rounded-pill bg-secondary\">P3
</span>
</span>
</th>
<td class=\"align-middle small\">DataScience</td>
<td class=\"align-middle small\">Finance</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>
My objective is to find the href link by changing "SomeCategory" value so that I may go to that page. The other 2 values stay the same. By changing "SomeCategory" value the number would change creating a new link. How would I find this href based on the category change while other filter tags remain the same so i can return the href value and create the link for that category like the one below and use driver to go to that page
https://mybooks.topics.com/repo/applications/328
(This link wont work its only for sample purpose)
CodePudding user response:
You can find the parent tr
element based on the SomeCategory
value and then find the a
element containing the href
inside it.
Something like this:
//tr[.//span[contains(.,'SomeCategory')]]//a
CodePudding user response:
To click() on the desired element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
With respect to the element with text DataScience:
text = "DataScience" WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//td[text()='{text}']//following-sibling::td[2]//a[@class='btn btn-sm btn-outline-primary']"))).click()
With respect to the element with text Finance:
text = "Finance" WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//td[text()='{text}']//following::td[1]//a[@class='btn btn-sm btn-outline-primary']"))).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