概述
启动Chrome浏览器,并链接到Baidu
IWebDriver chromeSession = new ChromeDriver(@"C:realUserToolresourcechromechromedriver_win32");
chromeSession.Navigate().GoToUrl("http://www.baidu.com");
获取窗口标题
string title = chromesession.title;
获取URL
String url = chromeSession.Url;
获取页面源码
string pagesource = chromeSession.PageSource;
清空 输入框内容
IWebElement element = chromeSession.FindElement(By.Id("")); element.SendKeys("Send Selenium Testing"); element.Clear();
获取页面的所有链接
foreach (var item in chromeSession.FindElements(By.TagName("a"))) { System.Windows.Forms.MessageBox.Show(item.GetAttribute("href")); }
点击指定按钮
IWebElement element = chromeSession.FindElement(By.Name("btn_Name")); element.Click();
查找元素
by.id
by.name
by.xpath
by.LinkText
by partialLinkText 链接中的部分文字
by TagName
by ClassName
执行 javascript
IJavaScriptExecutor js = chromeSession as IJavaScriptExecutor; string title = (string)js.ExecuteScript("return document.title");
弹出提示框
IJavaScriptExecutor js = chromeSession as IJavaScriptExecutor; js.ExecuteScript("alert('Hi this is alert')"); IAlert alert = chromeSession.SwitchTo().Alert(); alert.Accept();
前进和后退
IWebElement element = chromeSession.FindElement(By.LinkText("Privacy")); element.Click(); chromeSession.Navigate().Back(); chromeSession.Navigate().Forward();
最大化窗口
chromeSession.Manage().Window.Maximize();
获取元素坐标
int x = element.Location.X; int y = element.Location.Y;
打开新的tab
IWebElement element = chromeSession.FindElement(By.Id("")); element.SendKeys(OpenQA.Selenium.Keys.Control + "t");
刷新界面
chromeSession.Navigate().Refresh();
设置浏览器窗口的大小
System.Drawing.Size windowsize = new System.Drawing.Size(500, 350); chromeSession.Manage().Window.Size = windowsize;
右键菜单
IWebElement element = chromeSession.FindElement(By.Id("")); Actions builder = new Actions(chromeSession); builder.ContextClick(element).Build().Perform();
滚动桌面
IJavaScriptExecutor js = chromeSession as IJavaScriptExecutor; js.ExecuteScript("window.scrollBy(0,900);");
选择checkBox
选择radiobutton
IWebElement element = chromeSession.FindElement(By.Id("")); element.Click();
选择下拉菜单的值 dropdownbox
var option = chromeSession.FindElement(By.Id("")); var selectElement = new SelectElement(option); selectElement.SelectByText("");
发送值 SendKeys
IWebElement element = chromeSession.FindElement(By.Name("")); element.SendKeys(OpenQA.Selenium.Keys.Enter)
发送文字
IWebElement element = chromeSession.FindElement(By.Id("")); element.SendKeys("Selenium");
截图
System.Drawing System.Drawing.Design OpenQA.Selenium.Support.Events Screenshot ss = ((ITakesScreenshot)chromeSession).GetScreenshot(); ss.SaveAsFile("", System.Drawing.Imaging.ImageFormat.Png);
验证显示元素
IWebElement element = chromeSession.FindElement(By.Id("")); Console.WriteLine(element.Displayed);
areEqual 方法
using NUnit.Framework; try { Assert.AreEqual("Google", chromeSession.Title); Console.WriteLine("pass"); } catch (Exception e) { Console.WriteLine(e); }
断言按钮存在
try { Assert.IsTrue(chromeSession.FindElement(By.Id("")).Displayed); Console.WriteLine(""); } catch (Exception e) { Console.WriteLine(e); }
断言checkbox被选中
try { Assert.IsTrue(chromeSession.FindElement(By.Id("")).Selected); } catch (Exception) { throw; }
断言图片存在
try { Assert.IsTrue(chromeSession.FindElement(By.Id("")).Displayed); Console.WriteLine(""); } catch (Exception e) { Console.WriteLine(e); }
断言 pageSource
String html_page = chromeSession.PageSource; try { Assert.IsTrue(chromeSession.FindElement(By.TagName("body")).Text.Contains(html_page)); } catch (Exception e) { Console.WriteLine(e); }
Click button by javascript
IWebElement element = chromeSession.FindElement(By.Id(""));
((IJavaScriptExecutor)chromeSession).ExecuteScript("arguments[0].click();", element);
转载于:https://www.cnblogs.com/cooky/p/9462143.html
最后
以上就是儒雅糖豆为你收集整理的Selenium学习_常用场景代码示例的全部内容,希望文章能够帮你解决Selenium学习_常用场景代码示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复