【问题标题】:webdriver modal window click() not workingwebdriver模态窗口click()不起作用
【发布时间】:2012-12-04 10:31:10
【问题描述】:

我一直在四处寻找解决方案,但到目前为止没有一个解决方案对我有用。

这是我汇总的一个快速测试,它只是尝试单击“关闭”按钮来关闭模式弹出窗口。我可以在 Visual Studio 中逐步完成我的测试,它会正常工作。当我在 Nunit 中运行测试时,它会出错。我根据其他问题和给他们的建议尝试了以下方法:-

  • 到处排队等待
  • 从 chrome 驱动程序更改为 firefox
  • 更改为最大化窗口模式
  • 按照我能想到的方式重新设计它

模态框不是 iframe 或类似的东西。我似乎收到以下错误:

调用的目标已抛出异常。 ----> System.InvalidOperationException : 元素在点 (922.5, 342.0999755859375) 处不可点击。其他元素会收到点击:

这就是我摆弄最大化和正常大小模式的原因。

寻找任何建议,因为它让我很难过..

谢谢

[Test(Description = "Test to check if the cancel button closes the modal window when clicked on the 'Reset Password' modal")]
    public void CheckCancelPasswordResetOnModalWorks()
    {
        bool modalFoundSuccess = false;
        bool forgotPasswordControlFound = false;
        _driver.Navigate().GoToUrl(_baseURL + "login");

        if (_loginPage.CheckForgotPasswordControlExists())
        {
            forgotPasswordControlFound = true;

            _loginPage.ClickForgotPasswordButton();

            if (_loginPage.CheckResetPasswordModalIsDisplayed())
            {
                modalFoundSuccess = true;
                _loginPage.ClickCancelResetPasswordButton();
                if (_loginPage.CheckResetPasswordModalIsDisplayed() != true)
                {
                    modalFoundSuccess = false;
                }
                Assert.IsFalse(modalFoundSuccess, "The modal window did not close when the 'cancel' button was clicked on the modal pop up");
            }
            Assert.IsTrue(forgotPasswordControlFound, "Could not find the 'Forgotten Password' Modal box on the page");
        }
        Assert.IsTrue(forgotPasswordControlFound, "Was not able to find the 'Forgot Password' button on the '/login' page.");
    }

页面项目

public class LoginPage : Page
{
    private IWebDriver _driver;
    public string userNameValidationText = "Username must be filled in.";
    public string passwordValidationText = "Password must be filled in.";
    public string incorrectLoginValidationText = "The user name or password is incorrect";

    [FindsBy(How = How.ClassName, Using = "scfForm")]
    private IWebElement _WFFMForm;

    [FindsBy(How = How.XPath, Using = "//div[@class='scfSubmitButtonBorder']/input")]
    private IWebElement _loginButton;

    [FindsBy(How = How.XPath, Using = "//div[@class='scfSingleLineGeneralPanel']/input")]
    private IWebElement _userNameField;

    [FindsBy(How = How.XPath, Using = "//div[@class='scfPasswordGeneralPanel']/input")]
    private IWebElement _passwordField;

    [FindsBy(How = How.XPath, Using = "//div[@id='divForgotPassword']")]
    private IWebElement _resetPasswordModal;

    [FindsBy(How = How.XPath, Using = "//div[@id='divForgotPassword']/p/input")]
    private IWebElement _forgotPasswordEmailInputField;

    [FindsBy(How = How.XPath, Using = "//div[@id='divForgotPassword']/a[contains(., 'Reset My Password')]")]
    private IWebElement _resetPasswordButton;

    [FindsBy(How = How.XPath, Using = "//div[@id='divForgotPassword']/a[contains(., 'Cancel')]")]
    private IWebElement _cancelResetPasswordButton;

    [FindsBy(How = How.XPath, Using = "//div[@class='forgot-password']/a[contains(., 'Forgot Password')]")]
    private IWebElement _forgotPasswordButton;

    public LoginPage(IWebDriver driver)
        : base(driver)
    {
        _driver = driver;
        PageFactory.InitElements(_driver, this);
    }

    public void InputUserNameText(string phoneText)
    {
        _userNameField.Clear();
        _userNameField.SendKeys(phoneText);
    }

    public void InputPasswordText(string queryText)
    {
        _passwordField.Clear();
        _passwordField.SendKeys(queryText);
    }

    public void InputResetPasswordEmail(string resetEmail)
    {
        _forgotPasswordEmailInputField.Clear();
        _forgotPasswordEmailInputField.SendKeys(resetEmail);
    }

    public void ClickLoginButton()
    {
        _loginButton.Click();
    }

    public void ClickResetButton()
    {
        WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
        wait.Until((d) => { return CheckModalHasLoaded(); });
        _resetPasswordButton.Click();
    }

    public void ClickCancelResetPasswordButton()
    {
        WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(20));
        wait.Until((d) => { return CheckModalHasLoaded(); });
        _cancelResetPasswordButton.Click();
    }

    public void ClickForgotPasswordButton()
    {
        _forgotPasswordButton.Click();
    }

    public void ClickLoginButtonForEmtpyValidation()
    {
        _loginButton.Click();
        WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
        wait.Until((d) => { return CheckValidationTopBoxExists(); });
    }

    public bool CheckValidationForIncorrectLoginExists()
    {
        return Utility.IsThisElementPresent(_driver, By.XPath("//div[@class='scfSubmitSummary']/span"));
    }

    public bool loginFormExistsCheck()
    {
        return Utility.IsThisElementPresent(_driver, By.ClassName("scfForm"));
    }

    public bool CheckValidationTopBoxExists()
    {
        return Utility.IsThisElementPresent(_driver, By.ClassName("scfValidationSummary"));
    }

    public bool CheckResetPasswordModalIsDisplayed()
    {
        return Utility.IsThisElementPresent(_driver, By.XPath("//div[@id='divForgotPassword']"));
    }

    public bool CheckForgotPasswordControlExists()
    {
        return Utility.IsThisElementPresent(_driver, By.ClassName("forgot-password"));
    }

    public bool CheckModalHasLoaded()
    {
        return Utility.IsThisElementPresent(_driver, By.XPath("//div[@id='divForgotPassword']"));
    }

}

【问题讨论】:

  • 如果能提供一些html代码就好了?

标签: c# selenium webdriver


【解决方案1】:

如果模态框已经在 DOM 中(即未通过 ajax 加载),您可能需要更改它以等待元素可见(假设模态框已隐藏)。这是因为元素始终存在,只是不可见。这解释了为什么当您在调试模式下单步执行它时它也会起作用。

尝试使用类似的东西

WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[@id='ElementYouWantToTarget']")));

【讨论】:

  • 这似乎是问题所在。谢谢!
【解决方案2】:

对我来说,这听起来像是您的控件失去了焦点,或者您没有将注意力集中在正确的控件上。 您是如何找到合适的控件并专注于它的?

【讨论】:

  • 也许我需要像你说的那样设置焦点。我只是在等待模态显示,然后像处理任何其他页面一样使用选择器。您通常如何为模态设置焦点?不要以为你有这个链接 - 我还没有发现任何对 C# 和 webdriver 有帮助的谷歌搜索,这一切似乎都是过时的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多