空引用异常 - 使用Selenium Web驱动程序进行自动化测试

时间:2017-05-12 06:26:47

标签: c# selenium testing xpath selenium-webdriver

我可以在“登录”页面输入用户名,密码,然后可以导航到下一页;之后我想在获得Null Reference Exception时使用XPATH点击一个按钮。

基于我对此异常的研究的原因是Web驱动程序指向登录(上一页),因此无法在此(当前)页面中找到要单击的必需元素(按钮)。

我知道有一个内置方法switchTo使驱动程序指向此页面,但我不知道如何使用它。我通过谷歌搜索尝试了一些方法,但所有方法都失败了。

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

1 个答案:

答案 0 :(得分:0)

您可能需要找到并实现下面代码段中显示的界面。 根据目标浏览器,您可以选择一个驱动程序,在解决方案中将其作为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();
    }
}