【问题标题】:Read data from excel in Selenium Webdriver with c#使用 c# 从 Selenium Webdriver 中的 excel 中读取数据
【发布时间】:2017-06-07 10:02:23
【问题描述】:

我正在使用 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)' 有一些无效 CallSite.Target(Closure, CallSite, IWebElement, 对象)在 System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite 站点,T0 arg0,T1 arg1)

谁能帮忙解决这个问题?

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    好吧,您将Cells 类型的参数传递给SendKeys() 方法,而它需要string

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

    注意: 我假设你的单元格中有你需要的数据。

    【讨论】:

    • 试过 loginrange.Cells[emptyUsernameRowNumber][1].ToString(),但同样的异常
    • Convert.ToString(loginrange.Cells[emptyUsernameRowNumber][1].ToString()) 为我工作,但我得到的值是 System._ComObject。
    • 更新了答案。我试过了,现在应该可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多