【问题标题】:Webdriver timeout exception with elements inside divdiv内元素的W​​ebdriver超时异常
【发布时间】:2020-06-25 05:35:35
【问题描述】:

我尝试使用 Selenium WebDriver (C#) 在 YouTube 视频下点击评论区(添加公共评论)。

但是 Selenium 不能识别 id="header" 的 div 下的元素。 enter image description here 此代码正常工作:

    var element = driver
        .FindElement(By.Id("comments"))
        .FindElement(By.Id("header"));

但在这种情况下它返回空集合:

    var elements = driver
        .FindElement(By.Id("comments"))
        .FindElement(By.Id("header"))
        .FindElements(By.Id("placeholder-area"));

我已经尝试过使用框架的 driver.SwitchTo() 但它没有帮助

我也试过了

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));

        wait.Until(d => d
        .FindElement(By.Id("comments"))
        .FindElement(By.Id("header")).Displayed);

我得到了 Webdriver 超时异常enter image description here

我试过之后

       WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));

        wait.Until(d => d
        .FindElement(By.Id("comments"))
        .FindElement(By.Id("header")).Enabled);

        var element = driver.FindElement(By.Id("comments"))
        .FindElement(By.Id("header")).FindElement(By.Id("simple-box"));

我刚刚得到 NoSuchElementException for simple-box

       WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));

        wait.Until(d => d.FindElement(By.Id("comments"))
        .FindElement(By.Id("header")).FindElement(By.Id("simple-box")).Enabled);

我也尝试过使用当前元素的完整 xpath

        var element = driver
            .FindElement(By.XPath("//*[@id='simple-box']"));

        var element = driver.FindElement(By.XPath("/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[5]"));

【问题讨论】:

  • 您确定 HTML 结构符合您的期望吗?
  • 是的,我确定。我可以访问标题但无法访问简单框。我不知道为什么
  • 您可以在youtube视频页面查看并找到评论区(添加公共评论)

标签: c# selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

我刚刚尝试在 YouTube 中找到这些元素。看起来“cmets”部分没有完全加载。尝试先滚动到该部分:

var comments = driver.FindElement(By.Id("comments"));
((IJavaScriptExecutor)driver).ExecuteScript($"window.scrollTo({comments.Location.X}, {comments.Location.Y})");

然后显示等待标题。

【讨论】:

  • var element = driver.FindElement(By.Id("cmets")) .FindElement(By.Id("header"));动作动作=新动作(驱动程序); action.MoveToElement(元素); action.Perform();
猜你喜欢
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-29
相关资源
最近更新 更多