首页 > 解决方案 > Safari 12 中的硒元素位置

问题描述

我正在使用 BrowserStack 在一系列设备/浏览器上运行 Selenium 测试,包括 Safari 11,它运行良好。我现在正在尝试将 Safari 12 添加到测试中,但我从一开始就遇到了麻烦,因为我得到了:

System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'

一旦我尝试在第一个元素上获取位置。以下将在到达 Location 时失败,这是一个System.Drawing.Point.

IWebElement element = this.Driver.FindElement(byLocator);
int x = element.Location.X;

我该如何解决这个问题?

标签: c#seleniumsafaribrowserstack

解决方案


        /* Safari Hack */
        int x;
        int y;
        try
        {
            x = element.Location.X;
        }
        catch (Exception)
        {
            x = ((OpenQA.Selenium.Remote.RemoteWebElement)element).LocationOnScreenOnceScrolledIntoView.X;
        }
        try
        {
            y = element.Location.Y;
        }
        catch (Exception)
        {
            y = ((OpenQA.Selenium.Remote.RemoteWebElement)element).LocationOnScreenOnceScrolledIntoView.Y;
        }

推荐阅读