.net下Selenium2使用方法总结

一、Selenium简介
1.Selenium1(Selenium RC)   Selenium2(Selenium WebDriver)  Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)
2.WebDriver支持 Firefox (FirefoxDriver)、IE (InternetExplorerDriver)、Opera (OperaDriver) 和Chrome (ChromeDriver)

二、日常使用方法总结
1.生成一个web对象
IWebDriver driver;
driver = new FirefoxDriver();

2.跳转到指定页面
driver.Navigate().GoToUrl(baseURL + "/");
driver.title 取得当前页的title
driver.url 取得当前页的url

3.执行js脚本
((IJavaScriptExecutor) driver).ExecuteScript("js")

4.定位元素
    driver.FindElement(By.Id("cp1_btnModify"))

    By.ClassName(className))    
        By.CssSelector(selector)       
        By.Id(id)                     
        By.LinkText(linkText)          
        By.Name(name)             
        By.PartialLinkText(linkText)
        By.TagName(name)       
        By.Xpath(xpathExpression)

5.定位frame中元素
    (1)进入frame
    driver.SwitchTo().Frame("frame");

    (2)定位元素
    driver.FindElement(By.Id("div1"));
        driver.FindElement(By.Id("input1"));

    (3)退出frame
    driver.SwitchTo().DefaultContent();

6.如何得到弹出窗口
    (1)当前窗口的句柄
    driver.CurrentWindowHandle

    (2)所有窗口的句柄
    driver.WindowHandles

    (3)根据句柄得到窗口
    if driver.CurrentWindowHandle=driver.WindowHandles[i]
    IWebDriver window=driver.SwitchTo().Window(driver.WindowHandles[i])

    (4)根据窗口得到title,url
    window.Title
        window.Url

7.如何处理alert、confirm、prompt对话框
    (1)取得alert框信息
    Html代码:
    <input id = "alert" value = "alert" type = "button" onclick = "alert('欢迎!请按确认继续!');"/> 
    driver.FindElement(By.Id("alert")).Click();
        IAlert alert = driver.SwitchTo().Alert();
        Console.WriteLine(alert.Text);
    confirm.Dismiss(); //点弹框关闭

    (2)取得输出对话框上面的文字
    Html代码:
    <input id = "confirm" value = "confirm" type = "button" onclick = "confirm('确定吗?');"/>

    driver.FindElement(By.Id("confirm")).Click();
        IAlert confirm = driver.SwitchTo().Alert();
        Console.WriteLine(confirm.Text);
        confirm.Accept(); //点击确定

    (3)点击按钮,输入名字,然后点击确认
    Html代码:
    <input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('请输入你的名字:','请输入 
    你的名字'); document.write(name) "/>

    driver.FindElement(By.Id("prompt")).Click();
        IAlert prompt = driver.SwitchTo().Alert();
        Console.WriteLine(prompt.Text);
        prompt.SendKeys("Hello");
        prompt.Accept(); //点击确定

8.如何来处理select下拉框
    SelectElement selectCity=new SelectElement(driver.FindElement(By.Id("City")));

    (1)通过下拉框的索引选中第二项
        selectCity.SelectByIndex(2);
    (2)通过下拉列表中的选项的value属性选中
    selectCity.SelectByValue("10");

    (3)通过下拉列表中的选项的Text属性选中
    selectCity.SelectByText("北京");

    (4)遍历一下下拉列表所有选项,用click进行选中选项
    foreach(IWebElement e in selectCity.Options)
        {
           e.Click();
        }

9.如何操作cookie
    (1)增加一个name = "name",value="value"的cookie
    Cookie cookie=new Cookie("name","value");
        driver.Manage().Cookies.AddCookie(cookie);

    (2)得到页面下所有的cookies,输入它的所在域、name、value、有效日期、路径
    ICookieJar cookies=driver.Manage().Cookies;
        Cookie co = cookies.GetCookieNamed("name");
        Console.WriteLine(co.Domain);
        Console.WriteLine(co.Name);
        Console.WriteLine(co.Value);
        Console.WriteLine(co.Expiry);
        Console.WriteLine(co.Path);
    (3)删除cookie三种方法
    a)通过cookie的name
    driver.Manage().Cookies.DeleteCookieNamed("CookieName");

    b)通过cookie对象
    driver.Manage().Cookies.DeleteCookie(cookie);

    c)全部删除
    driver.Manage().Cookies.DeleteAllCookies();
10.如何等待页面元素加载完成
    (1)明确等待
    (2)隐形等待

11.如何利用Selenium-webdriver截图
    Thread.Sleep(5000);
        Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot();
        screenShotFile.SaveAsFile("test",ImageFormat.Jpeg);

12.如何取得table中的内容
    (1)通过行得到列的方法
    private IWebElement GetCell(IWebElement row,int cell)
        {
            IList<IWebElement> cells;
            IWebElement target = null;
            //列里面有"<th>"、"<td>"两种标签,所以分开处理
            if(row.FindElements(By.TagName("th")).Count>0)
            {
                cells = row.FindElements(By.TagName("th"));
                target = cells[cell];
            }

            if(row.FindElements(By.TagName("td")).Count>0)
            {
                cells = row.FindElements(By.TagName("td"));
                target = cells[cell];
            }
            return target;
        }

    (2)通过By得到行的方法
    public String GetCellText(By by,String tableCellAddress)
        {
            //得到table元素
            IWebElement table = driver.FindElement(by);
            //对所要查找的单元格位置字符进行分解,得到对应的行、列
            int index = tableCellAddress.Trim().IndexOf('.');
            int row = Convert.ToInt32(tableCellAddress.Substring(0, index));
            int cell = Convert.ToInt32(tableCellAddress.Substring(index + 1));
            //得到table表中所有行对象,并得到所要查询的行对象
            IList<IWebElement> rows = table.FindElements(By.TagName("tr"));
            IWebElement theRow = rows[row];
            return GetCell(theRow, cell).Text;
        }

    (3)通过参数得到对应行列的内容
    Console.WriteLine(GetCellText(By.Id("mytable"),"0.2")); //得到id="mytable"中的第一行第三列的表格内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值