C# selenium 实例-批量下载word文章
感悟:
1、用chrome好处之一就是:可以直接从开发工具中复制XPath,然后用在C#字符串前加上@,并将字符串当中每一个双引号都换成两个双引号即可,比较方便。
2、用selenium的好处就是在frame间切换比较方便。
场景:
我用的OA就是frame套iframe,感觉稍微难点就是frame切换,但是找到button点击就可以下载word了。
费了半天劲,虽然代码比较粗糙,但是终于可以从OA一次下载10篇文章了。
代码:
private void 批量处理待办ToolStripMenuItem_Click(object sender, EventArgs e) { var driver = new OpenQA.Selenium.Chrome.ChromeDriver(); driver.Navigate().GoToUrl("http://192.168.132.80/login/Login.jsp"); driver.FindElement(By.Id("loginid")).SendKeys("admin"); driver.FindElement(By.Id("userpassword")).SendKeys("1234"); driver.FindElement(By.Id("login")).Click(); WebDriverWait explicitWait = new WebDriverWait(driver, TimeSpan.FromSeconds(6)); explicitWait.Until(d => d.FindElement(By.Id("mainFrame"))); driver.SwitchTo().Frame("mainFrame"); driver.FindElement(By.Id("more_655")).Click();//more_1560 ////*[@id="more_655"] driver.SwitchTo().Window(driver.WindowHandles[1]); System.Threading.Thread.Sleep(2000); ReadOnlyCollection<IWebElement> eles = driver.FindElements(By.XPath("//a[starts-with(@href,'javaScript:openFullWindowHaveBarForWFList')]")); //这里无需循环,点第一个即可,后续操作会自动刷新窗口 eles[0].Click(); System.Threading.Thread.Sleep(2000); driver.SwitchTo().Window(driver.WindowHandles[2]); //frame1: 还有bodyiframe driver.SwitchTo().Frame("bodyiframe"); System.Threading.Thread.Sleep(2000); //frame2 页面中的编辑控件中,还有iframe //*[@id="cke_contents_remark"]/iframe IWebElement frameED = driver.FindElement(By.XPath(@"//*[@id=""cke_contents_remark""]/iframe")); driver.SwitchTo().Frame(frameED); ///html driver.FindElement(By.XPath(@"/html")).SendKeys("同意"); driver.SwitchTo().ParentFrame().SwitchTo().DefaultContent(); driver.FindElement(By.XPath(@"//*[@id=""toolbarmenu""]/td[1]/table/tbody/tr[2]/td[2]/em/button")).Click();////*[@id="toolbarmenu"]/td[1]/table/tbody/tr[2]/td[2]/em/button System.Threading.Thread.Sleep(10000); driver.Quit();//在完成操作后,同时关闭浏览器窗口和Weddriver会话 } private void 批量下载已阅新闻ToolStripMenuItem_Click(object sender, EventArgs e) { var driver = new OpenQA.Selenium.Chrome.ChromeDriver(); driver.Navigate().GoToUrl("http://192.168.132.80/login/Login.jsp"); driver.FindElement(By.Id("loginid")).SendKeys("admin"); driver.FindElement(By.Id("userpassword")).SendKeys("1234"); driver.FindElement(By.Id("login")).Click(); System.Threading.Thread.Sleep(1000); //*[@id="_ButtonCancel_0"] try { driver.FindElement(By.XPath(@"//*[@id=""_ButtonCancel_0""]")).Click(); } catch { } //主页 流程 driver.FindElement(By.XPath(@"//*[@id=""head""]/tbody/tr/td[2]/table/tbody/tr/td/div/div[1]/div[4]")).Click(); System.Threading.Thread.Sleep(3000); //左侧已办 //*[@id="drillmenu"]/ul/li[3]/a driver.FindElement(By.XPath(@"//*[@id=""drillmenu""]/ul/li[3]")).Click(); //进入frame1 mainFrame /html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[1]/table/tbody/tr/td/div[3]/ul/li[7] driver.SwitchTo().Frame("mainFrame"); System.Threading.Thread.Sleep(3000); ReadOnlyCollection<IWebElement> anchors = driver.FindElements(By.XPath(@"//a")); foreach (IWebElement a in anchors) { if (a.Text.Contains("XX审批流程()")) { a.Click(); break; } } //循环开始 for (int k = 0; k < 11; k++) { ////*[@id="_xTable"]/div[1]/div[2]/table/tbody/tr[1]/td[6]/a ReadOnlyCollection<IWebElement> oo = driver.FindElements(By.XPath("//a[contains(@href,'javaScript:openFullWindowHaveBarForWFList')]")); int count = oo.Count; for (int i = 0; i < count ; i++) { //开新窗口1 oo[i].Click(); System.Threading.Thread.Sleep(1000); int j = i + 1; //切换到新窗口 driver.SwitchTo().Window(driver.WindowHandles[1]); System.Threading.Thread.Sleep(2000); //还有bodyiframe driver.SwitchTo().Frame("bodyiframe"); //下载按钮 //*[@id="selectDownload"]/nobr/button // driver.FindElement(By.XPath(@"//button[@class=""btnFlowd""]")).Click(); driver.FindElement(By.TagName(@"button")).Click(); System.Threading.Thread.Sleep(5000); //关闭下载页,即当前窗口 driver.Close(); //循环后要切回窗口0 因为关闭窗口1后,也没有自动回到窗口0的mainFrame中!且oo也无法循环了 driver.SwitchTo().Window(driver.WindowHandles[0]); driver.SwitchTo().Frame("mainFrame"); oo = driver.FindElements(By.XPath("//a[contains(@href,'javaScript:openFullWindowHaveBarForWFList')]")); } //循环后切回窗口0,点下一页, System.Threading.Thread.Sleep(8000); driver.SwitchTo().Window(driver.WindowHandles[0]); System.Threading.Thread.Sleep(2000); driver.SwitchTo().Frame("mainFrame"); driver.FindElement(By.Id(@"-weaverTable-0-next")).Click();//*[@id="-weaverTable-0-next"] System.Threading.Thread.Sleep(2000); } //循环结束 driver.Quit();//在完成操作后,同时关闭浏览器窗口和Weddriver会话 }