Selenium的简单使用
Selenium的简单使用
1 static void test1(IWebDriver driver) 2 { 3 4 driver.Url = "https://i.cnblogs.com/files"; 5 log.Info("打开网页"); 6 //跳转登录页 7 Thread.Sleep(100); 8 log.Info("跳转登录页,等待登录"); 9 10 //登录完成后 11 while (true) 12 { 13 if (driver.Url == "https://i.cnblogs.com/files") 14 { 15 log.Info("登录完毕,进入操作页面"); 16 var ele_upload = driver.FindElement(By.XPath("/html/body/cnb-root/cnb-layout/div[2]/div[3]/div[2]/div/cnb-spinner/div/cnb-files/cnb-collapse-panel[1]/div[2]/cnb-spinner/div/div[2]/cnb-form-field/div/div/input")); 17 log.Info("找到文件框 ele_upload"); 18 ele_upload.SendKeys("d:\\test.zip"); 19 log.Info("设置要上传的文件 ele_upload"); 20 var ele_btn = driver.FindElement(By.XPath("/html/body/cnb-root/cnb-layout/div[2]/div[3]/div[2]/div/cnb-spinner/div/cnb-files/cnb-collapse-panel[1]/div[2]/cnb-spinner/div/div[2]/div[1]/button")); 21 log.Info("找到上传按钮 ele_btn"); 22 ele_btn.Click(); 23 log.Info("点击上传按钮 ele_btn"); 24 log.Info("操作页面完毕"); 25 Thread.Sleep(100); 26 break; 27 } 28 } 29 30 }
XPath参考: https://www.cnblogs.com/shuchang1001/p/13563931.html