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();
  }

}
原文地址:https://www.cnblogs.com/wangyinxu/p/6404106.html