1

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...

2
  • yes you can do it in a lambda expression Commented Jul 3, 2020 at 8:07
  • Could you please elaborate a bit more? I'm not yet familiar with lambda expressions. Commented Jul 3, 2020 at 8:15

2 Answers 2

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}']"))
Sign up to request clarification or add additional context in comments.

Comments

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
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.