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
C# 无法使用Selenium WebDriver自动登录Azure Active Directory_C#_Selenium_Selenium Webdriver_Azure Active Directory - Fatal编程技术网

C# 无法使用Selenium WebDriver自动登录Azure Active Directory

C# 无法使用Selenium WebDriver自动登录Azure Active Directory,c#,selenium,selenium-webdriver,azure-active-directory,C#,Selenium,Selenium Webdriver,Azure Active Directory,我正在尝试使用Azure AD测试登录到我们的web应用程序,但登录按钮上的单击操作似乎没有注册。代码如下: namespace WebApp.Tests { using System; using System.IO; using System.Threading; using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Se

我正在尝试使用Azure AD测试登录到我们的web应用程序,但登录按钮上的单击操作似乎没有注册。代码如下:

namespace WebApp.Tests
{
    using System;
    using System.IO;
    using System.Threading;
    using FluentAssertions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;

    [TestClass]
    public class LoginTests
    {
        [TestMethod]
        public void CanLogin()
        {
            string internetExplorerDriverServerDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
            IWebDriver driver = new InternetExplorerDriver(internetExplorerDriverServerDirectory)
            {
                Url = "https://localhost:44399"
            };

            try
            {
                driver.Manage().Cookies.DeleteAllCookies();

                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
                wait.Until(d => d.FindElement(By.Id("logonIdentifier")));

                driver.FindElement(By.Id("logonIdentifier")).SendKeys("username");
                driver.FindElement(By.Id("password")).SendKeys("password");   
                driver.FindElement(By.Id("next")).Click();

                wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
                wait.Until(d => d.FindElement(By.ClassName("logo_title")));

                driver.FindElement(By.ClassName("logo_title")).Text.Should().Contain("HELLO!");
            }
            finally
            {
                driver.Close();
                driver.Quit();
                driver.Dispose();
                driver = null;
            }
        }
    }
}
以下是HTML的相关部分:

<div class="entry">
    <div class="entry-item">
        <label for="logonIdentifier">Email Address</label>
        <div class="error itemLevel" aria-hidden="true" style="display: none;">
            <p role="alert"></p>
        </div>
        <input type="email" id="logonIdentifier" name="Username or email address" pattern="^[a-zA-Z0-9.!#$%&amp;’'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$" placeholder="Email Address" value="" tabindex="1" autocomplete="off">
    </div>
    <div class="entry-item">
        <div class="password-label">
            <label for="password">Password</label>
                <a id="forgotPassword" tabindex="2" href="/redacted">Forgot your password?</a>
        </div>
        <div class="error itemLevel" aria-hidden="true" style="display: none;">
            <p role="alert"></p>
        </div>
        <input type="password" id="password" name="Password" placeholder="Password" tabindex="1" autocomplete="off">
    </div>
    <div class="working"></div>
    <div class="buttons">
        <button id="next" tabindex="1">Sign in</button>
    </div>
</div>

电子邮件地址

密码

登录
在浏览器中看起来一切正常:用户名和密码字段已填充,按钮看起来像被点击了一样(小“工作”图标在其上方短暂出现),但什么也没有发生

  • 我尝试了更长的等待时间(最长30秒)来打开主页 单击“登录”按钮后出现
wait=newwebdriverwait(驱动程序,TimeSpan.FromSeconds(30))

  • 我尝试在表单中发送
    Enter
    键,而不是单击 按钮
driver.FindElement(By.Id(“密码”)).SendKeys(Keys.Enter)

  • 我尝试执行一些JavaScript来调用按钮单击操作
IJavaScriptExecutor jse=(IJavaScriptExecutor)驱动程序
jse.ExecuteScript(“document.getElementById('next')。单击();”

  • 我试过使用Chrome和Firefox驱动程序
  • 我已经在本地开发版本和托管测试环境上进行了尝试
到目前为止,一切都不起作用。在此过程中,浏览器中不会显示任何错误/验证消息

我对这个有点不知所措

谢谢,
斯图尔特。

自2019-01-20年<代码>起,这对我有效:

[ClassInitialize]
public static void InitializeClass(TestContext testContext)
{
    var options = new ChromeOptions();
    options.AddArguments("--incognito");
    driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
}

[TestMethod]
public void Login()
{
    driver.Navigate().GoToUrl("https://localhost:44399/");
    driver.FindElement(By.Id("i0116")).Clear();
    driver.FindElement(By.Id("i0116")).SendKeys("test@test.onmicrosoft.com");
    driver.FindElement(By.Id("i0116")).SendKeys(Keys.Enter);
    driver.FindElement(By.Id("i0118")).Clear();
    driver.FindElement(By.Id("i0118")).SendKeys("password");
    Thread.Sleep(500);
    driver.FindElement(By.Id("i0118")).SendKeys(Keys.Enter);
    driver.FindElement(By.Id("idSIButton9")).Click();
}

我已经通过使用CSS选择器作为元素定位器解决了这个问题

SeleniumLibrary。输入文本css:#i0118您的_密码


它在每次迭代中都能正常工作。

在运行Selenium测试时,您是否使用开发人员工具进行了检查?我们正是在我们的项目中这样做的,而且它是有效的。我们使用的id是
idSIButton9
使用相关的HTML@rickvdbosch,是的,在断点处检查开发人员工具中的源代码。Button肯定有next的ID。将问题编辑为包含HTML。@StuartWhiteford也用您的代码试用版更新问题