【问题标题】:Null Reference Exception - while using Selenium Web Driver for Automated testing空引用异常 - 使用 Selenium Web 驱动程序进行自动化测试时
【发布时间】:2017-05-12 20:14:23
【问题描述】:

我可以在登录页面输入用户名、密码并导航到下一页;之后,我尝试在出现空引用异常时使用 XPATH 单击按钮。

根据我对这个异常的研究,原因是网络驱动程序指向登录(上一个)页面,因此在这个(当前)页面中找不到需要点击的元素(按钮)。

我知道有一个内置方法 switchTo 可以使驱动程序指向此页面,但我不知道如何使用它。我在google上搜索了一些方法,但都失败了。

如何使用switchTo方法让驱动指向当前页面?

【问题讨论】:

  • 我不知道你的意思,我用过硒,但我真的没有。如果没有代码,只有您解释您的研究,我们将无法为您提供帮助
  • 我想将驱动的焦点设置到当前弹出的窗口。当我点击登录按钮时,会被带到一个新的 URL 对吗?我想将 Web 驱动程序的焦点设置为那个新的(当前)URL。你现在明白了吗?这里没有太多代码可以分享。
  • Selenium 应该打开浏览器并自行导航,然后它会自行跟踪所有内容(如当前页面)

标签: c# selenium testing xpath selenium-webdriver


【解决方案1】:

可能你需要找到并实现下面代码sn-p所示的接口。 根据目标浏览器,您可以选择一个驱动程序,将其作为 nuget 包包含在您的解决方案中,并且在包中,通常应该有一个实现此接口的类。示例驱动程序 nuget 包是 Selenium.WebDriver.ChromeDriver

namespace OpenQA.Selenium
{
    //
    // Summary:
    //     Defines an interface allowing the user to access the browser's history and to
    //     navigate to a given URL.
    public interface INavigation
    {
        //
        // Summary:
        //     Move back a single entry in the browser's history.
        void Back();
        //
        // Summary:
        //     Move a single "item" forward in the browser's history.
        //
        // Remarks:
        //     Does nothing if we are on the latest page viewed.
        void Forward();
        //
        // Summary:
        //     Load a new web page in the current browser window.
        //
        // Parameters:
        //   url:
        //     The URL to load. It is best to use a fully qualified URL
        //
        // Remarks:
        //     Calling the OpenQA.Selenium.INavigation.GoToUrl(System.String) method will load
        //     a new web page in the current browser window. This is done using an HTTP GET
        //     operation, and the method will block until the load is complete. This will follow
        //     redirects issued either by the server or as a meta-redirect from within the returned
        //     HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait
        //     until this timeout is over, since should the underlying page change while your
        //     test is executing the results of future calls against this interface will be
        //     against the freshly loaded page.
        void GoToUrl(string url);

        void GoToUrl(Uri url);
        //
        // Summary:
        //     Refreshes the current page.
        void Refresh();
    }
}

【讨论】:

    【解决方案2】:

    以下代码行用于使驱动程序切换到当前打开的窗口。

    SPCCommonValues.Driver.SwitchTo().Window(SPCCommonValues.Driver.WindowHandles.Last());

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多