【问题标题】:Selenium not inputing strings based on name?Selenium 不根据名称输入字符串?
【发布时间】:2019-01-05 15:23:09
【问题描述】:

我正在尝试使用 selenium 在 C# Visual Studio 中创建一些测试。当我运行测试时,谷歌浏览器导航到指定的网站,但是没有在用户名和密码字段中输入任何键/字符串...

这是为什么?我该如何解决?如何创建一个让用户输入正确的用户名和密码查询并单击提交表单的测试。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;


namespace test
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            IWebDriver driver = new ChromeDriver("/Users/name/Desktop/test/");

            driver.Navigate().GoToUrl("http://newtours.demoaut.com/mercurywelcome.php");

            IWebElement username = driver.FindElement(By.Name("userName"));
            username.SendKeys("mercury");

            IWebElement password = driver.FindElement(By.Name("password"));
            password.SendKeys("mercury");

            var signIn = driver.FindElement(By.Name("login"));
            signIn.Submit();


        }
    }
}

【问题讨论】:

    标签: c# selenium testing selenium-webdriver automated-tests


    【解决方案1】:

    页面加载需要一些时间。所以,请在导航到 URL 后添加一些等待。

    在您的测试中添加以下 NuGet 包并修改如下代码

    NuGet 包: DotNetSeleniumExtras.WaitHelpers

    附加命名空间声明,

    using OpenQA.Selenium.Support.UI; //for the wait method
    using SeleniumExtras.WaitHelpers;  //for the expected conditions method
    

    工作代码:

        driver.Navigate().GoToUrl("http://newtours.demoaut.com/mercurywelcome.php");
    
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
    
        wait.Until(ExpectedConditions.TitleContains("Welcome"));//It will wait until the page load completion 
    
        IWebElement username = driver.FindElement(By.Name("userName"));
        username.SendKeys("mercury");
    

    【讨论】:

    • @Novice_29:请检查答案。如果这个答案有帮助,请点击勾选箭头按钮接受答案,并点击向上箭头按钮投赞成票
    猜你喜欢
    • 2023-03-14
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    相关资源
    最近更新 更多