Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium C#-如何告诉Selenium,我们已被重定向到另一个页面?(ADFS认证)_C#_Selenium_Redirect - Fatal编程技术网

Selenium C#-如何告诉Selenium,我们已被重定向到另一个页面?(ADFS认证)

Selenium C#-如何告诉Selenium,我们已被重定向到另一个页面?(ADFS认证),c#,selenium,redirect,C#,Selenium,Redirect,我最近开始使用SeleniumWebDriver。很快,我就被以下问题所阻碍 我想测试一个平台,它首先要求用户使用Microsoft帐户登录,然后用户被重定向到公司的ADFS页面。在用户输入他的凭证后,他最终登录到所需页面 基本上,我成功地使用MS凭据登录用户,并被重定向到ADFS页面。问题是,当我被重定向时,Selenium无法识别我在不同的页面上,因此不可能进行进一步的测试…我看到了一个带有帧的解决方案,但这不适用于这种情况,因为这里没有帧 迄今为止的代码: [SetUp]

我最近开始使用SeleniumWebDriver。很快,我就被以下问题所阻碍

我想测试一个平台,它首先要求用户使用Microsoft帐户登录,然后用户被重定向到公司的ADFS页面。在用户输入他的凭证后,他最终登录到所需页面

基本上,我成功地使用MS凭据登录用户,并被重定向到ADFS页面。问题是,当我被重定向时,Selenium无法识别我在不同的页面上,因此不可能进行进一步的测试…我看到了一个带有帧的解决方案,但这不适用于这种情况,因为这里没有帧

迄今为止的代码:

        [SetUp]
        public void Initialize()
        {

            //Go to desired page
            driver.Navigate().GoToUrl("https://test.test/");
           //User gets redirected to MS login platform
        }

        [Test]
        public void ExecuteTest()
        {
            //Writing email and clicking on next in order to re-direct to ADSF
            IWebElement element = driver.FindElement(By.Name("element1"));
            element.SendKeys("test@test.com");
            driver.FindElement(By.Id("button")).Click();

            //Trying to write credentials in the ADSF page                
            driver.FindElement(By.Name("element2"));
            element.SendKeys("test@test.com");
            driver.FindElement(By.Name("element3"));
            element.SendKeys("password");
            driver.FindElement(By.Id("Button2")).Click();

        }
结果跟踪:

   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id)
   at OpenQA.Selenium.By.<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)

   Result Message:  
   OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"id","selector":"passwordInput"}
在OpenQA.Selenium.Remote.RemoteWebDriver.unpackanthrowonerror(Response error Response)
在OpenQA.Selenium.Remote.RemoteWebDriver.Execute(stringdrivercommandtoexecute,Dictionary`2参数)
在OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(字符串机制,字符串值)
位于OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(字符串id)
在OpenQA.Selenium.By.c__DisplayClass16_0.b_0(ISearchContext上下文)中
在OpenQA.Selenium.By.FindElement(ISearchContext)上
位于OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By)
结果消息:
OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“id”,“选择器”:“passwordInput”}

更新:在尝试了下面提到的所有内容并检查了一些关于ADSF授权的文章后,我认为问题不是因为没有等待页面加载/错误的ID、名称等,或者类似的东西。。我假设ADSF页面本身受到自动化保护,必须找到另一种解决方案。

登录后,请尝试以下操作-

driver.SwitchTo().Window(driver.WindowHandles.Last());

我假定您遇到了“元素未找到”或“元素不可见”错误。您可以检查页面是否正确,也可以尝试按如下方式等待页面完全加载:

       public void WaitForPageToBeFullyLoaded(int timeSpan)
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(timeSpan)).Until(
            d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
    }
其他选项是等待元素可见或可单击:

    public IWebElement WaitForCondition(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondition,
        double timeout)
    {
        var webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));

        return webDriverWait.Until(expectedCondition);
    }
public IWebElement WaitForCondition(IWebDriver驱动程序,Func expectedCondition,
双超时)
{
var webDriverWait=新的webDriverWait(驱动程序,TimeSpan.FromSeconds(超时));
返回webDriverWait.Until(预期条件);
}

能否添加尝试在ADFS页面中输入凭据时获得的堆栈跟踪。我添加了“Result StackTrace”
NoTouchElementException
正在引发。因此,在元素可见之前执行
.SendKeys
。在这种情况下,您必须使用显式等待(如@nikolaybarakov所示)。或者
.Name
是动态的(从外观上看,似乎是动态的)。在这种情况下,请更改定位器并重试。我建议在这里使用XPath。Selenium知道您在另一个页面上,但您的脚本没有。您需要添加代码来检测浏览器所在的页面以及在该页面上执行的操作。遗憾的是,Selenium仍然找不到所需字段的Id(也尝试了名称)…请在登录后尝试获取页面的当前URL-driver.getcurrenturl()。我认为它应该打印第一页url,这意味着驱动程序仍在获取上一页。您可以尝试刷新页面驱动程序.navigate().refresh(),也可以按照其他注释中的说明等待。让我知道。在尝试了这两种方法之后,它仍然会抛出一个找不到Id的错误。这是目前最接近正常工作的方法…当我使用这两种建议中的一种时,它在执行时不会抛出错误。问题是它仍然没有在ADSF页面上的凭据字段中写入任何内容..;/(换句话说,如果您确定id不是动态的,并且每次访问页面时都会更改,请尝试使用className、cssSelector或xpath