How to perform the Explicit Wait method In Selenium with C Sharp?
Hey Ana,
The Explicit Wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an “ElementNotVisibleException
” exception.
This can be achieved with the combination of WebDriverWait
and ExpectedConditions
.
WebDriverWait
by default calls ExpectedCondition
to poll by every 500 milliseconds until it returns successfully.
Here’s the code snippet:
public static void ExplicitWait(WebDriver driver, WebElement element) {
(new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOf(element));
}
The results are in the iframe so, switch to it and then get the .page_source
iframe = driver.find_element_by_css_selector("#mainContent iframe")
driver.switch_to.frame(iframe)
I also added a wait for the table to be loaded:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
#locate and switch to the iframe
iframe = driver.find_element_by_css_selector("#mainContent iframe")
driver.switch_to.frame(iframe)
#wait for the table to load
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'companyName')))
print(driver.page_source)