I would like to click on the dynamic URL using selenium python from a web page. I have the following HTML where 123456 is dynamic text linked to dynamic URL. I am not able to use driver.find_element_by_link_text()
as text also dynamic. Can someone please help me with this?
<td ><a href="xyz.jsp?serviceID=123456=">123456</a></td>
Note: Both URL and Text also dynamic
CodePudding user response:
If the DOM line stays, but only the link changes, then you can use other attributes like below (assuming that a
enclosed in the class resultsColumn
)
By XPATH
driver.find_element(By.XPATH, "//td[@class='resultsColumn']//a")
CodePudding user response:
To click() on the dynamic URL using Selenium and python you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "td.resultsColumn > a[href*='serviceID']").click()
Using xpath:
driver.find_element(By.XPATH, "//td[@class='resultsColumn']/a[contains(@href, 'serviceID')]").click()
Ideally to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.resultsColumn > a[href*='serviceID']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='resultsColumn']/a[contains(@href, 'serviceID')]"))).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