webdriver---API---(java版) 6

1、判断页面元素是否存在/可用

package cn.gloryroad;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
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 ApiTest3 {
    public WebDriver driver;
    String baseUrl;
  @Test
  public void f() {
      baseUrl="http://www.sogou.com";
      driver.get(baseUrl);
      WebElement inputBox=driver.findElement(By.id("query"));
      if(inputBox.isEnabled()==true){
          inputBox.sendKeys("sogou 页面搜索输入框已经被找到");
      }else{
          Assert.fail("页面上输入框元素未被找到!");
      }
  }
  @BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "C:\chromedriver\chromedriver.exe");
      driver=new ChromeDriver();
  }

  @AfterMethod
  public void afterMethod() {
      try{
          Thread.sleep(3000);
      }catch(InterruptedException e){
          e.printStackTrace();
      }
      driver.quit();
  }

}

2、操作新弹出的浏览器窗口

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.NoSuchWindowException;
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 ApiTest3 {
    public WebDriver driver;
    String baseUrl;
  @Test
  public void f() {
      baseUrl="http://www.sogou.com";
      driver.get(baseUrl);
      String parentWindowHandle=driver.getWindowHandle();
      WebElement inputBox=driver.findElement(By.xpath(".//*[@id='QRcode-footer']/div[2]/a[3]"));
      
          inputBox.click();
      
      Set<String>allwindowsHandles= driver.getWindowHandles();
      if(!allwindowsHandles.isEmpty()){
          for(String windowHandle : allwindowsHandles){
              try{
                  if(driver.switchTo().window(windowHandle).getPageSource().contains("春装上新"))
                  {
                      driver.findElement(By.id("f81")).click();
                  }
              }catch(NoSuchWindowException e){
                  e.printStackTrace();
              }
          }
      }
      driver.switchTo().window(parentWindowHandle);
      driver.findElement(By.id("query")).sendKeys("回到搜狗首页!");
  }
  @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();
  }

}

 3、操作javaScript的Alert、confirm、prompt弹窗

package cn.gloryroad;

import org.testng.annotations.Test;

import java.io.File;

import org.testng.annotations.BeforeMethod;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchWindowException;
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 ApiTest3 {
    public WebDriver driver;
    String baseUrl;
  @Test
  public void f() {
     File file=new File("D:\workspace\WebDriver-API\src\alert.html");
     String filePath=file.getAbsolutePath();
     driver.get(filePath);
     WebElement button =driver.findElement(By.xpath("//input"));
     button.click();
     try{
         Alert alert=driver.switchTo().alert();//获得alert元素
         Assert.assertEquals("这是一个alert弹出框", alert.getText());
         alert.accept();//点击确定   alert.dismiss();在prompt、confirm弹出框点击取消  
         //alert.sendKeys();方法,在prompt弹出框输入内容   alert弹出框在页面点击操作时,可能看不到,可以加一个输出操作
     }catch(NoAlertPresentException e){
         e.printStackTrace();
     }
  }
  @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/6402271.html