selenium上传文件

using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Runtime.InteropServices;

namespace eip_auto_test
{
    
    class BatchCreateProductBomWeight
    {
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public static void Test()
        {
            ChromeOptions option = new ChromeOptions();
            option.AddArguments("disable-infobars");

            ChromeDriver cd = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory, option);

            cd.SwitchTo().Window(cd.WindowHandles[cd.WindowHandles.Count - 1]).Manage().Window.Maximize();
            cd.Navigate().GoToUrl(Common.server_url);
            cd.FindElementById("txtName").SendKeys("test");
            cd.FindElementById("txtPassword").SendKeys("123");
            cd.FindElementById("BtnLogin").Click();

            cd.FindElementByLinkText("Data").Click();

            cd.FindElementByLinkText("Customer").Click();

            cd.FindElementByLinkText("Weight List").Click();

            IWebElement iframe = cd.FindElement(By.Id("main"));
            cd.SwitchTo().Frame(iframe);


            IWebElement element = cd.FindElement(By.Id("fileBrowe"));            
            IWebElement file = cd.FindElement(By.Id("btnBrowse"));            
            file.Click();


            Common.Delay(500);
            keybd_event(27, 0, 0, 0);            
            keybd_event(27, 0, 2, 0);


            element.SendKeys(@"C:mydocCustom.xlsx");
            cd.ExecuteScript("$('#fileBrowe').change()");

            cd.FindElement(By.Id("btnUpload")).Click();
            


            Console.ReadLine();
            cd.Quit();
        }

      
    }
}

  

循环测试的版本

     static void Main(string[] args)
        {
            for(int zz=1;zz<20;zz++)
                BatchCreateProductBomWeight.Test();
        }

  

using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Runtime.InteropServices;

namespace eip_auto_test
{
    
    class BatchCreateProductBomWeight
    {
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public static void Test()
        {
            ChromeOptions option = new ChromeOptions();
            option.AddArguments("disable-infobars");

            ChromeDriver cd = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory, option);

            cd.SwitchTo().Window(cd.WindowHandles[cd.WindowHandles.Count - 1]).Manage().Window.Maximize();
            cd.Navigate().GoToUrl(Common.server_url);
            cd.FindElementById("txtName").SendKeys("test");
            cd.FindElementById("txtPassword").SendKeys("123");
            cd.FindElementById("BtnLogin").Click();

            cd.FindElementByLinkText("Data").Click();

            cd.FindElementByLinkText("Customer").Click();

            cd.FindElementByLinkText("Weight List").Click();

            IWebElement iframe = cd.FindElement(By.Id("main"));
            cd.SwitchTo().Frame(iframe);


            IWebElement element = cd.FindElement(By.Id("fileBrowe"));            
            IWebElement file = cd.FindElement(By.Id("btnBrowse"));            
            file.Click();


            Common.Delay(500);
            keybd_event(27, 0, 0, 0);            
            keybd_event(27, 0, 2, 0);


            element.SendKeys(@"c:mydoccustomer.xlsx");
            cd.ExecuteScript("$('#fileBrowe').change()");

            cd.FindElement(By.Id("btnUpload")).Click();


            IWebElement state = cd.FindElement(By.Id("myState0"));

            Console.WriteLine(state.Text);

            while (state.Text != "完成")
            {
                Common.Delay(1000);
            }

            Common.Delay(2000);
            cd.Quit();
        }

      
    }
}

  

原文地址:https://www.cnblogs.com/coolyylu/p/8327836.html