Selenium C Webdriverwait Not Waiting

Find the data you need here

We provide programming data of 20 most popular languages, hope to help you!

Previous PostNext Post

Selenium / C# WebDriverWait Not Waiting

The above code clicks on the openCloseWrap (Language Selection Button) and then I am trying to pause for seconds (100) and then trying to click on the SP flag to change the language. WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100)); // wait.Until(By.Id("ButtonSPFlag")); IWebElement element = wait.Until(driver

 //check spanish
 driver.FindElement(By.XPath("//*[@id='openCloseWrap']/img")).Click();
 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
 driver.FindElement(By.Id("ButtonSPFlag")).Click();

 String check = driver.FindElement(By.XPath("html/body/form/div[5]/div/div[1]/div/p")).Text;
 Console.Out.WriteLine(check);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
//    wait.Until(By.Id("ButtonSPFlag"));
IWebElement element = wait.Until(driver => driver.FindElement(By.Id("ButtonSPFlag")));
element.Click();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
DoesElementExistX(driver, sw, "//*[@id='openCloseWrap']/img");
DoesElementExistX(driver, sw, "//*[@id='ButtonUSFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtoFRFlag']");
DoesElementExistX(driver, sw, "//*[@id='ImageButton1']");
DoesElementExistX(driver, sw, "//*[@id='ButtonDEFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtonITFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtonSPFlag']");

    #region[DoesElementExistX]
    public static void DoesElementExistX(IWebDriver driver, StreamWriter sw, String id)
    {
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
        try
        {
            wait.Until(ExpectedConditions.ElementExists(By.XPath(id)));
        }
        catch (WebDriverTimeoutException)
        {
            sw.WriteLine("FAILED - " + id);
        }
    }
    #endregion
driver.FindElement(By.CssSelector(".topMenuAction")).Click();
driver.FindElement(By.Id("ButtonSPFlag")).Click();
String Check = driver.FindElement(By.CssSelector("#loginBoxInner>p")).Text;

Selenium Easy

We can use WebDriverWait class in many different cases. When ever we need to perform any operation on element, we can use Webdriver wait to check if the element is Present or visible or enabled or disabled or Clickable etc. NOTE - There are changes with Waits and Timeouts in Selenium 4, please check Updated Webdriver waits and timeouts. We will look …

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 
WebElement element = driver.findElements(By.cssSelector(""));
element.size()>0;
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.elementToBeClickable(locator));
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebDriverWait wait = new WebDriverWait(driver, waitTime);
 wait..until(ExpectedConditions.visibilityOf(element));
List<WebElement> linkElements = driver.findelements(By.cssSelector('#linkhello'));
WebDriverWait wait = new WebDriverWait(driver, waitTime);
 wait..until(ExpectedConditions.visibilityOfAllElements(linkElements));
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator)); 
WebElement element = driver.findElement(By.id(""));
element.isEnabled();
WebElement element = driver.findElement(By.id(""));
element.isDisplayed();
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementWithText(by));
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementWithText(by, strText));

WebDriverWait in Selenium

In Selenium wait commands mainly used for synchronization purpose because some times WebDriver does not track the active web element or real state of DOM. For example driver navigate to a page but throw no such element exception because of synchronization issue. WebDriverWait is the type of explicit wait. There are two types of explicit wait.

WebDriverWait  wait = new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.elementToBeClickable(By.id(“Some id”)));
package waits;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExplicitWaits {
    public static void main(String[] args) {
        //To create a new instance of Firefox Driver
        WebDriver driver = new ChromeDriver();
        //To open a website "Automation Testings"
        driver.get("https://automationtestings.com/");
        //To maximize the browser window
        driver.manage().window().maximize();
        //This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds
        WebDriverWait wait = new WebDriverWait (driver, 10);
        //Title of the webpage is "Learn Automation | Automation Skills"
        wait.until(ExpectedConditions.titleIs("Learn Automation | Automation Skills "));
        //If the above condition met then the browser will be closed
        //To close the browser
        driver.close();
        //Change the title " Learn Automation | Automation Skills " as "xyz" in the script and try
        //You will face an execption - Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for title to be "Software Testing Material - A site for Software Tes". Current title: "xyz"
    }
}
WebDriverWait  wait = new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.elementToBeClickable(By.id(“Some id”)));

Cannot find WebDriverWait class in OpenQA.Selenium (C#)

(Migrating from Java-Selenium to C#-Selenium) When searching for explicit waits with Selenium and C# I find several posts with code that looks similar to the Java-Counterpart:. for example here:. WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5)); wait.Until(By.Id("login"));

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
wait.Until(By.Id("login"));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("locator")));
using OpenQA.Selenium;
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.46.0</version>
</dependency>
using OpenQA.Selenium.Support.UI;
    using OpenQA.Selenium;
    using SeleniumExtras.WaitHelpers;
        
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
  wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("Test")));
 **Doesn't work** 
   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='... ']"))

**Doesn't work** 
   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h3[@class='... ']"));

 **Works**
   Thread.sleep(3000);
   driver.findElement(By.xpath("//h3[@class='... ']");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[@class='... ']"))
WebDriverWait wait1 = new WebDriverWait(driver, 15); 
    wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[@class='‌​..‌​. ']"))); 
driver.findElement(By.xpath("//h3[@class='... ']")).click();
    WebDriverWait wait2 = new WebDriverWait(driver, 15); 
        wait2.until(ExpectedConditions.elementToBeSelected(By.xpath("//h3[@class='‌​..‌​. ']"))); 
    driver.findElement(By.xpath("//h3[@class='... ']")).click();
 new FluentWait<WebDriver>(getDriver())
         .withTimeout(Integer.parseInt(time), TimeUnit.SECONDS)
         .pollingEvery(100, TimeUnit.MILLISECONDS)
         .ignoring(NoSuchElementException.class)
         .ignoring(StaleElementReferenceException.class)
         .until(new ExpectedCondition<WebElement>() {

          public WebElement apply(WebDriver driver) {
           return driver.findElement(locator);
      }
         });

Previous PostNext Post

Related code examples