Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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# OpenQA.Selenium.NoSuchElementException:没有这样的元素_C#_Visual Studio_Selenium_Specflow_Gherkin - Fatal编程技术网

C# OpenQA.Selenium.NoSuchElementException:没有这样的元素

C# OpenQA.Selenium.NoSuchElementException:没有这样的元素,c#,visual-studio,selenium,specflow,gherkin,C#,Visual Studio,Selenium,Specflow,Gherkin,我试图通过使用Gherkin格式通过Specflow自动化测试用例,但我一直有相同的错误: OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“*[name=“customer_firstname”]” 文本框的名称是customer_firstname,我不明白为什么它会显示此错误 我正在测试的网站是: 小黄瓜档案: Feature: Register in the website Sc

我试图通过使用Gherkin格式通过Specflow自动化测试用例,但我一直有相同的错误:

OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“*[name=“customer_firstname”]”

文本框的名称是customer_firstname,我不明白为什么它会显示此错误

我正在测试的网站是:

小黄瓜档案:

Feature: Register in the website

  Scenario Outline: Register a new user
    Given That I am on the Register page
    And The website will start by entering 
    | email              |
    | gti19001@demo2.com |
    When User enters their credentials
     | firstName | lastName | password     |
     | DEmo      | Demo     | 12345        |
    When The client will enter 
      | Address              | City      | State | Zip   | MobilePhone |
      | Fake adress, 12, efj | Something |     1 | 35242 | +1 5893246 4863 |
    Then Click on the register button
步骤定义文件为:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
using WebsiteTestingSpecflow.Pages;

namespace WebsiteTestingSpecflow.Steps
{
    [Binding]
    public sealed class RegisterStep
    {

        RegisterPage registerPage = null;

        [Given(@"That I am on the Register page")]
        public void GivenThatIAmOnTheRegisterPage()
        {
            IWebDriver webDriver = new ChromeDriver();
            webDriver.Navigate().GoToUrl("http://automationpractice.com/index.php?controller=authentication&back=my-account");
            registerPage = new RegisterPage(webDriver);
        }

        [Given(@"The website will start by entering")]
        public void GivenTheWebsiteWillStartByEntering(Table table)
        {
            dynamic data = table.CreateDynamicInstance();

            registerPage.Register(data.email.ToString());
 registerPage.VerifyEmail();
        }

        [When(@"User enters their credentials")]
        public void WhenUserEntersTheirCredentials(Table table)
        {
            dynamic data = table.CreateDynamicInstance();

            registerPage.RegisterCredentials(data.firstName.ToString(), data.lastName.ToString(), data.password.ToString());
        }


        [When(@"The client will enter")]
        public void WhenTheClientWillEnter(Table table)
        {
            dynamic data = table.CreateDynamicInstance();

            registerPage.RegisterInformation(data.Address.ToString(), data.City.ToString(), data.State.ToString(), data.Zip.ToString(), data.MobilePhone.ToString());
        }

        [Then(@"Click on the register button")]
        public void ThenClickOnTheRegisterButton()
        {
            registerPage.RegisterBtn();
        }
    }
    }
实现这些功能的文件是:

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Text;

namespace WebsiteTestingSpecflow.Pages
{
    class RegisterPage
    {
        public IWebDriver Webdriver { get; }

        public RegisterPage(IWebDriver webDriver)
        {
            Webdriver = webDriver;
        }


        //UI Elements of Registration
        public IWebElement txtEmailRegister => Webdriver.FindElement(By.Name("email_create"));

        public IWebElement btnRegisterVerify => Webdriver.FindElement(By.CssSelector("#SubmitCreate > span"));

        public IWebElement txtName => Webdriver.FindElement(By.Name("customer_firstname"));

        public IWebElement txtSurname => Webdriver.FindElement(By.Name("customer_lastname"));

        public IWebElement txtPassword => Webdriver.FindElement(By.Name("passwd"));

        public IWebElement txtAddress => Webdriver.FindElement(By.Name("address1"));

        public IWebElement txtCity => Webdriver.FindElement(By.Name("city"));

        public IWebElement txtState => Webdriver.FindElement(By.Name("id_state"));

        public IWebElement txtZIP => Webdriver.FindElement(By.Name("postcode"));

        public IWebElement txtPhone => Webdriver.FindElement(By.Name("phone_mobile"));

        public IWebElement btnRegister => Webdriver.FindElement(By.CssSelector("#submitAccount > span"));


        public void Register(string Email)
        {
            txtEmailRegister.SendKeys(Email);
            
        }

        public void RegisterCredentials(string Username, string Lastname, string password)
        {

            txtName.SendKeys(Username);
            txtSurname.SendKeys(Lastname);
            txtPassword.SendKeys(password);
        }

        public void RegisterInformation(string address, string city, string State, string zip, string phone)
        {
            txtAddress.SendKeys(address);
            txtCity.SendKeys(city);
            txtState.SendKeys(State);
            txtZIP.SendKeys(zip);
            txtPhone.SendKeys(phone);
        }

        public void VerifyEmail() => btnRegisterVerify.Submit();

        public void RegisterBtn() => btnRegister.Submit();
    }
}
测试输出:

有人知道为什么会这样吗


提前感谢您。

您可能需要添加一些等待。
尝试添加

wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Name("customer_firstname")));
以前

txtName.SendKeys(Username);

找不到该元素,因为您需要完成当前页面的自动化

  • 请注意,您需要单击“创建帐户”按钮

  • 这是微妙的,但页面会淡入。当它淡入时,元素无法交互,因此需要等待

  • 您需要进行以下更改:

  • 带有电子邮件注册文本字段的页面不是注册页面。这是主页,因此我将为此创建一个页面模型:

    public class HomePage
    {
        private readonly IWebDriver driver;
        private readonly WebDriverWait wait;
    
        private IWebElement EmailInput => driver.FindElement(By.Name("email_create"));
        private IWebElement CreateAccountButton => driver.FindElement(By.Name("SubmitCreate"));
    
        public HomePage(IWebDriver driver)
        {
            this.driver = driver;
            this.wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        }
    
        public RegisterPage Register(string email)
        {
            EmailInput.SendKeys(email);
            CreateAccountButton.Click();
            wait.Until(ExpectedConditions.StalenessOf(By.TagName("body")));
    
            return new RegisterPage(driver);
        }
    }
    
  • 修改RegisterPage以等待某些元素可单击,以说明页面在淡入时不可交互:

    public class RegisterPage
    {
        private readonly WebDriverWait wait;
    
        public IWebDriver Webdriver { get; }
    
        // UI elements ....
    
        public RegisterPage(IWebDriver webDriver)
        {
            Webdriver = webDriver;
            wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
        }
    
        public void RegisterCredentials(string Username, string Lastname, string password)
        {
            // Wait for the page to fade in
            wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("customer_firstname")));
            txtName.SendKeys(Username);
            txtSurname.SendKeys(Lastname);
            txtPassword.SendKeys(password);
        }
    
        public void RegisterInformation(string address, string city, string State, string zip, string phone)
        {
            // Wait for the page to fade in so you are not required
            // to call RegisterCredential before RegisterInformation.
            wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("address1")));
            txtAddress.SendKeys(address);
            txtCity.SendKeys(city);
            txtState.SendKeys(State);
            txtZIP.SendKeys(zip);
            txtPhone.SendKeys(phone);
        }
    }
    

  • 在输入电子邮件地址后是否发生了什么事情?您能否将您的问题包含在HTML的代表性示例中?@GregBurghardt我已在问题中包含了指向我正在测试的网站的链接。通常情况下,在我输入电子邮件并按下“创建帐户”按钮后,它会将我重定向到另一个页面,但不会发生。可能是因为没有识别按钮?输入电子邮件后,您从未按下按钮进入下一页。从开始链接的页面首先要求创建或登录。您需要点击创建帐户first@JoshAdams是的,你是对的,我忘了输入按钮的输入方法。我确实修复了它,但我仍然遇到了与异常相同的问题。
    txtName
    属性本身正在引发异常,它看起来像。有意义:)请参阅更新的答案。可能需要注意的是,这应该是一个不同的页面对象,而不是注册页面。“创建帐户”或“登录”页面应该是“自己的”,而“注册”页面应该是“自己的”。