繁体   English   中英

使用C#从Selenium Webdriver中的excel读取数据

[英]Read data from excel in Selenium Webdriver with c#

我正在使用Selenium Webdriver C#。 我需要从Excel读取数据。

码:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using Excel = Microsoft.Office.Interop.Excel;



namespace UIL
{
    [TestClass]
    public class UIL_Login : UtilityHelper
    {
        IWebDriver driver;
        Excel.Application loginapp;
        Excel.Workbook loginworkbook;
        Excel._Worksheet loginworksheet;
        Excel.Range loginrange;

        public UIL_Login()
        {
            loginapp = new Excel.Application();
            loginworkbook = loginapp.Workbooks.Open(@"D:\\MyProjects\\Selenium_C#\\SeleniumProjects\\UIL\\UIL\\Resources\\login.xls");
            //loginworksheet = loginworkbook.Sheets[1];
            loginworksheet = (Excel.Worksheet)loginworkbook.Worksheets.get_Item(1);
            loginrange = loginworksheet.UsedRange;
        }

        [TestInitialize]
        public void SetUp()
        {
            driver = new ChromeDriver(@"D:\\MyProjects\\Selenium_C#\\Selenium\\chromedriver");
            driver.Navigate().GoToUrl("http://192.168.0.35:92");
            driver.Manage().Window.Maximize();
        }

        /*Login without username*/
        [TestMethod]
        public void loginWithoutUsername()
        {
            try
            {
                int emptyUsernameRowNumber = 1;
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
                IWebElement username = webElement(driver, Constants.VAR_EMAIL);
                username.Clear();
                //username.SendKeys("amrutha.u@teamta.in");
                username.SendKeys(loginrange.Cells[emptyUsernameRowNumber][1]);
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
                IWebElement password = webElement(driver, Constants.VAR_PASSWORD);
                password.Clear();
                // password.SendKeys("UIL@123#");
                password.SendKeys(loginrange.Cells[emptyUsernameRowNumber][2]);
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
                IWebElement signinButton = driver.FindElement(By.XPath("//*[@id='mainDiv']/div[4]/div/div/form/a/button"));
                signinButton.Click();
            }
            catch (Exception e)
            {
                Console.Write(e);

            }

        }
    }
}

但是我收到以下异常:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:最佳重载方法匹配'OpenQA.Selenium.IWebElement.SendKeys(string)'在System.Dynamic.UpdateDelegates的CallSite.Target(Closure,CallSite,IWebElement,Object)具有一些无效的参数。 UpdateAndExecuteVoid2 [T0,T1](CallSite网站,T0 arg0,T1 arg1)

有人可以帮助解决这个问题吗?

好吧,当您期望使用string时,您可以将Cells类型的参数传递给SendKeys()方法。

尝试loginrange.Cells[emptyUsernameRowNumber][1].Value2.ToString()

注意:我假设您已在单元格中拥有所需的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM