【问题标题】:How to detect if a page is overflowing horizontally with Selenium Driver?如何使用 Selenium Driver 检测页面是否水平溢出?
【发布时间】:2013-09-25 06:33:11
【问题描述】:

我正在使用 Selenium 加载网页。我需要检测页面是否水平溢出。不幸的是,driver.FindElement(By.TagName("html")).Size.Width 总是返回浏览器宽度,即使实际的 HTML 元素溢出。

检查是否存在水平滚动条会起作用。反正有没有检查?

还有其他想法吗?

感谢 C# 代码示例。

谢谢,

【问题讨论】:

    标签: c# javascript html selenium


    【解决方案1】:

    我不懂C#,但它的本质是在页面上执行以下JQuery:

    $("body").width() >= $(window).width()
    

    并将其转换为布尔值;

    【讨论】:

      【解决方案2】:

      作为 MrTi 答案的更新,您可以使用此 C# 代码:

       var js = (IJavaScriptExecutor)webDriver;
       var isOverflowing = js.ExecuteScript("$('body').width() >= $(window).width()");
      

      如果窗口溢出,则返回布尔值

      【讨论】:

      • 谢谢,这让我走上了正确的道路。但这不起作用。 $('body').width() 不返回实际的正文宽度,只返回可见部分。
      • 明确一点:MCM的解决方案在stackoverflow.com/a/18922093/334862
      【解决方案3】:

      这行得通,不需要 jQuery。

      bool isOver = (bool)js.ExecuteScript("javascript: return document.getElementsByTagName(\"html\")[0].scrollWidth > document.getElementsByTagName(\"html\")[0].clientWidth") ;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-28
        • 2017-01-16
        • 2022-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-16
        • 2013-05-07
        相关资源
        最近更新 更多