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

C# 空引用异常-使用Selenium Web驱动程序进行自动测试时,c#,selenium,testing,xpath,selenium-webdriver,C#,Selenium,Testing,Xpath,Selenium Webdriver,我可以在登录页面中输入用户名、密码,并可以导航到下一页;之后,我试图在获取Null引用异常时使用XPATH单击一个按钮 根据我的研究,出现此异常的原因是web驱动程序指向登录(上一页)页面,因此无法在此(当前)页面中找到需要单击的元素(按钮) 我知道有一个内置的方法可以让驱动指向这个页面,但我不知道如何使用它。我尝试了一些在谷歌搜索的方法,但都失败了 如何使用switchTo方法使驱动程序指向当前页面?您可能需要找到下面代码片段中所示的接口并实现它。 根据目标浏览器的不同,您可以选择一个驱动程序

我可以在登录页面中输入用户名、密码,并可以导航到下一页;之后,我试图在获取Null引用异常时使用XPATH单击一个按钮

根据我的研究,出现此异常的原因是web驱动程序指向登录(上一页)页面,因此无法在此(当前)页面中找到需要单击的元素(按钮)

我知道有一个内置的方法可以让驱动指向这个页面,但我不知道如何使用它。我尝试了一些在谷歌搜索的方法,但都失败了


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

您可能需要找到下面代码片段中所示的接口并实现它。 根据目标浏览器的不同,您可以选择一个驱动程序,将其作为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();
    }
}

以下代码行用于使驾驶员切换到当前打开的车窗


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

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