C sharp - Esperar por un elemento (webdriver) con c#

 
Vista:
sin imagen de perfil
Val: 3
Ha aumentado 1 puesto en C sharp (en relación al último mes)
Gráfica de C sharp

Esperar por un elemento (webdriver) con c#

Publicado por David (2 intervenciones) el 26/11/2019 11:04:25
Buenos días:

Escribo en el foro a ver si alguien sabe como puedo esperar por un elemento programando en c#, estoy usando webdriver de selenium pero no hay manera que espere por un elemento en páginas que cargan por partes (en este caso hecha en angular). He probado varias formas pero nada, la única que funciona pero no quiero usar es Thread.Sleep(1000);

He probado las esperas implicitas y explicitas, también las esperas fluent, os pongo algunos ejemplos que he hecho en un código a ver si alguien sabe como puedo llegar a una solución.

Comentar por si acaso que el elemento aunque le ponga Displayed o Enabled no funciona, ya que el elemento está activo y desplegado, pero al intentar hacer click falla, porque tiene que esperar un segundo para poder hacer click

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static WebDriverWait GetWebDriverWait(this IWebDriver webDriver)
        {
            var waitForTime = new WebDriverWait(webDriver, TimeSpan.FromSeconds(20));
            return waitForTime;
        }
public static void GetWebDriverImplicitWait(this IWebDriver webDriver, int time)
        {
            webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(time);
        }
 public static IWebElement GetFluentWaitIdElement(this IWebDriver webDriver,string date)
        {
            DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(webDriver);
            fluentWait.Timeout = TimeSpan.FromSeconds(10);
            fluentWait.PollingInterval = TimeSpan.FromSeconds(0.5);
            fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            var fluentWaitId = fluentWait.Until(x => x.FindElement(By.Id(date)));
            return fluentWaitId ;
        }
 public static IWebElement WaitId(this IWebDriver webDriver, string dateId)
        {
            var waitId = GetWebDriverWait(webDriver).Until(d => d.FindElement(By.Id(dateId)));
            return waitId;
        }
 
//Esto va dentro de un método claro :)
driver.GetWebDriverWait().Until(ExpectedConditions.ElementToBeClickable(By.Id("el elemento que sea")));
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder