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...
Asked
Active
Viewed 1,116 times
1

undetected Selenium
- 183,867
- 41
- 278
- 352

Morello
- 135
- 1
- 1
- 11
-
yes you can do it in a lambda expression – edisonmecaj Jul 03 '20 at 08:07
-
Could you please elaborate a bit more? I'm not yet familiar with lambda expressions. – Morello Jul 03 '20 at 08:15
2 Answers
2
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}']"))

edisonmecaj
- 1,062
- 1
- 9
- 20
2
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

undetected Selenium
- 183,867
- 41
- 278
- 352