예제 #1
0
        private IWebElement GetElement(TimeSpan?timeout = null)
        {
            IWebElement elem = null;
            //var wait = new WebDriverWait(WebDriver, timeout ?? new TimeSpan(0, 0, 20));
            var WebDriverWait = new WebDriverWait(WebDriver, new TimeSpan(0, 0, 20));

            try
            {
                if (Predicate == null)
                {
                    elem = WebDriverWait.Until(ExpectedConditions.ElementExists(By.XPath(XPath)));
                }
                else
                {
                    var elements = WebDriverWait.Until(dr => dr.FindElements(By.XPath(XPath)));
                    foreach (var el in elements)
                    {
                        var elementContext = new WebElementContext {
                            Element = el
                        };
                        if (Predicate(elementContext))
                        {
                            elem = elementContext.Element;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Could not get element");
            }
            return(elem);
        }
예제 #2
0
        public bool Exists(TimeSpan?timeout = null)
        {
            var wait = new WebDriverWait(WebDriver, timeout ?? new TimeSpan(0, 0, 20));

            if (Predicate == null)
            {
                try
                {
                    var foundElement = wait.Until(dr =>
                    {
                        try
                        {
                            return(dr.FindElement(By.XPath(XPath)));
                        }
                        catch
                        {
                            return(null);
                        }
                    });

                    var tagName = foundElement?.TagName;
                    Console.WriteLine(tagName);
                    return(!string.IsNullOrWhiteSpace(tagName));
                }
                catch
                {
                    return(false);
                }
            }

            var elements = wait.Until(dr => dr.FindElements(By.XPath(XPath)));

            foreach (var el in elements)
            {
                var elementContext = new WebElementContext {
                    Element = el
                };
                if (Predicate(elementContext))
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #3
0
        public bool Click(TimeSpan?timeout = null)
        {
            WaitForVisibility();
            IWebElement elem = null;

            var wait = new WebDriverWait(WebDriver, timeout ?? new TimeSpan(0, 0, 20));

            if (Predicate != null)
            {
                var elements = wait.Until(dr => dr.FindElements(By.XPath(XPath)));
                foreach (var el in elements)

                {
                    var cl = new WebElementContext {
                        Element = el
                    };

                    if (Predicate(cl))
                    {
                        elem = cl.Element;
                        break;
                    }
                }
            }
            else
            {
                elem = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(XPath)));
            }
            if (elem != null && elem.Enabled)

            {
                try
                {
                    elem.Click();
                }
                catch (Exception)
                {
                    ((IJavaScriptExecutor)WebDriver).ExecuteScript("window.ScrollTo(0" + elem.Location.Y + ")");
                    //Sleep.HalfSecond();
                }
                return(true);
            }
            return(false);
        }