【问题标题】:VB.NET Selenium question about webdriverwait to wait for specific element been load by browserVB.NET Selenium 关于 webdriverwait 等待浏览器加载特定元素的问题
【发布时间】:2021-05-05 06:06:23
【问题描述】:

我一直在努力寻找如何等待特定元素被加载到浏览器。 但是,大部分代码都在 python 和 C# 中,我正在尝试转换但仍然不能。

  1. 我找不到 ExpectedConditions 请让我知道我需要导入哪个引用,因为我一直在命名空间中寻找 is expectedCondidtion 仍然找不到它。

  2. 谁能分享如何使用 webdriverwait 的代码或链接?因为我还是无法理解。 我想做的是等待一个元素被加载到浏览器中,如果元素不在页面中,则会执行其他操作。 现在我使用的是try and catch,但是有时候网速慢不能真正等待页面完全加载,直接显示元素异常不在页面中。

代码:

 WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
 WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("MainContent_lblLandmarkUPRN")));

【问题讨论】:

    标签: vb.net selenium selenium-webdriver


    【解决方案1】:

    您可以使用下面的函数等待元素可见

     public static IWebElement WaitforElement(this IWebDriver driver, By by, int timeoutInSeconds = 5, bool checkIsVisible = false)
        {
            IWebElement element;
            driver.Sync();
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
            try
            {
                if (checkIsVisible)
                {
                    element = wait.Until(ExpectedConditions.ElementIsVisible(by));
                    element = wait.Until(ExpectedConditions.ElementToBeClickable(by));
                  }
                else
                {
                    element = wait.Until(ExpectedConditions.ElementExists(by));
                }
            }
            catch (NoSuchElementException) { element = null; }
            catch (WebDriverTimeoutException) { element = null; }
            catch (TimeoutException) { element = null; }
            catch (StaleElementReferenceException) { element = null; }return element;
        }
    

    【讨论】:

    • 非常感谢,我会试试的。顺便说一句,OpenQA 中所需的能力在哪里。 Selenium 命名空间?
    • 命名空间 OpenQA.Selenium.Remote { // 用于创建 OpenQA.Selenium.IWebDriver 所需的浏览器功能的类。 // 如果您希望使用默认值,请使用静态方法 public class DesiredCapabilities : ICapabilities { }
    • @BlueGene 命名空间 OpenQA.Selenium.Remote
    • 非常感谢。请问Chrome Devtool在哪里?我想使用 CDP 命令来模拟 ip 定位的时区和地理位置。我看了好久
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多