For example, I'm opening page https://example.com/page-1/ in Selenium, looking for a specific link that contains domain.com. Right now I'm using sleep(20) to ensure the page has fully loaded. But I wonder if I can use WebDriverWait not only for tag presence but also for its contains presence as well. Could not find any solution yet...
-
yes you can do it in a lambda expressionedisonmecaj– edisonmecaj2020-07-03 08:07:17 +00:00Commented Jul 3, 2020 at 8:07
-
Could you please elaborate a bit more? I'm not yet familiar with lambda expressions.Morello– Morello2020-07-03 08:15:50 +00:00Commented Jul 3, 2020 at 8:15
Add a comment
|
2 Answers
You can try something like this, I'm assuming that you are using Firefox but the logic is the same:
firefox = webdriver.Firefox()
firefox.get('https://example.com/page-1/')
#wait for a maximum of 60 seconds in this example
wait = WebDriverWait(firefox, 60)
domain = "domain.com"
wait.until(lambda x: x.find_element_by_css_selector(f"a[href*='{domain}']"))
Comments
To wait for the WebElement for the href attribute to contain a specific string, you can induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Full match of
domain.com:Using
CSS_SELECTOR:element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href='domain.com']")))Using
XPATH:element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@href='domain.com']")))
Partial match of
domain.com:Using
CSS_SELECTOR:element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href*='domain.com']")))Using
XPATH:element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@href, 'domain.com')]")))
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