• webdriver---API---(java版) 7


    1、操作frame中的页面元素

    <html>
    <head>
      <title>frameset 页面</title>
    </head>
    <frameset cols="25%,50%,25%">
        <frame id ="leftframe"     src="frame_left.html" />
        <frame id ="middleframe"   src="frame_middle.html" />
        <frame id ="rigthframe"    src="frame_right.html" />
    </frameset>
    </html>
    
    
    
    <html>
    <head>
      <title>左侧 frame</title>
    </head>
    <body>
       <p>这是左侧frame页面上的文字</p>
    </body>
    </html>
    
    
    <html>
    <head>
      <title>中间 frame</title>
    </head>
    <body>
       <p>这是中间frame页面上的文字</p>
    </body>
    </html>
    
    <html>
    <head>
      <title>右侧 frame</title>
    </head>
    <body>
       <p>这是右侧frame页面上的文字</p>
    </body>
    </html>

    <html>
    <head>
    <title>iframe</title>
    </head>
    <body>
    <p>跳动的手指</p>
    </body>
    </html>

     
    package cn.gloryroad;
    
    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeMethod;
    import java.io.File;
    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.Assert;
    import org.testng.annotations.AfterMethod;
    
    public class ApiTest4 {
        public  WebDriver driver;
        String baseUrl;
      @Test
      public void f() {
          File file=new File("D:\workspace\WebDriver-API\src\frame.html");
          String filepath=file.getAbsolutePath();
          driver.get(filepath);
          List<WebElement>frames=driver.findElements(By.tagName("frame"));
          for(WebElement frame:frames){
              driver.switchTo().frame(frame);//进入frame
              if(driver.getPageSource().contains("中间 frame")){
                  WebElement middleText=driver.findElement(By.xpath("//p"));
                  System.out.println(middleText.getText());
                  Assert.assertEquals("这是中间frame页面上的文字", middleText.getText());
                  break;
              }else{
                  driver.switchTo().defaultContent();//退出当前frame,返回frameset页面
              }
          }
          driver.switchTo().defaultContent();
      }
      @BeforeMethod
      public void beforeMethod() {
          System.setProperty("webdriver.chrome.driver","C:\chromedriver\chromedriver.exe");
          driver=new ChromeDriver();
      }
    
      @AfterMethod
      public void afterMethod() {
          try{
              Thread.sleep(5000);
          }catch(InterruptedException e){
              e.printStackTrace();
          }
      }
    
    }

    2、操作iFrame中页面元素

     List<WebElement>frames=driver.findElements(By.tagName("frame"));
          for(WebElement frame:frames){
              driver.switchTo().frame(frame);//进入frame
              if(driver.getPageSource().contains("左侧 frame")){
                  WebElement myiframe=driver.findElement(By.tagName("iframe"));
                  driver.switchTo().frame(myiframe);
                  WebElement leftIframeText=driver.findElement(By.xpath("//p"));
                  System.out.println(leftIframeText.getText());
                  Assert.assertEquals("跳动的手指", leftIframeText.getText());
                  break;
              }else{
                  driver.switchTo().defaultContent();//退出当前frame,返回frameset页面
              }
          }
          driver.switchTo().defaultContent();

    3、操作浏览器的cookie,自动登录

    package cn.gloryroad;
    
    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeMethod;
    import java.util.Set;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Cookie;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.Assert;
    import org.testng.annotations.AfterMethod;
    
    public class ApiTest4 {
        public  WebDriver driver;
        String baseUrl;
      @Test
      public void f() {
        baseUrl="http://www.baidu.com";
        driver.get(baseUrl);
        Set<Cookie>cookies= driver.manage().getCookies();
        System.out.println(cookies);
        driver.manage().deleteAllCookies();
    //    Cookie newCookie=new Cookie("cookieName", "cookieValue");
    //    System.out.println(String.format("Domain -> name -> value -> expiry -> path"));
    //    for(Cookie cookie:cookies){
    //        System.out.println(String.format("%s -> %s -> %s -> %s -> %s", cookie.getDomain(),cookie.getName(),cookie.getValue(),cookie.getPath()));
    //    }
    
    //    addCookie(Cookie cookie)。添加cookie,参数是Cookie对象
    //    deleteAllCookies。删除所有cookie
    //    getCookies。返回所有的cookie
    //    deleteCookieNamed(String name)。删除name这个cookie
    //    getCookieNamed(String name)。返回特定name的cookie值
         Cookie c1 = new Cookie("BAIDUID", "账号");
         Cookie c2 = new Cookie("BDUSS", "密码");
         driver.manage().addCookie(c1);
         driver.manage().addCookie(c2);
        
      }
      @BeforeMethod
      public void beforeMethod() {
          System.setProperty("webdriver.chrome.driver","C:\chromedriver\chromedriver.exe");
          driver=new ChromeDriver();
      }
    
      @AfterMethod
      public void afterMethod() {
          try{
              Thread.sleep(5000);
          }catch(InterruptedException e){
              e.printStackTrace();
          }
          driver.quit();
      }
    
    }
  • 相关阅读:
    HDU-1215 七夕节 数论 唯一分解定理 求约数之和
    LightOJ-1259 Goldbach`s Conjecture 数论 素数筛
    [前端-动态数据可视化]横向柱状图的动态数据可视化
    CodeForces-722C Destroying Array 并查集 离线操作
    CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除
    CodeForces-1007A Reorder the Array 贪心 田忌赛马
    POJ-3692 Kindergarten 二分图 最大团
    个人开发者做一款Android App需要知道的事情
    android各种组件的监听器
    留言处插入xss不弹框
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6404106.html
Copyright © 2020-2023  润新知