• selenium webdriver窗口切换(上)


    selenium webdriver窗口切换,有时候在做自动化的时候需要打开很多很多的页面,

    当在操作不同的页面的时候需要切换窗口,下面是如何切换到前后页面窗口的操作:

    package test20161206;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    
    public class chuangkouqiehuan20161206 {
    
     public static void main(String[] args) throws InterruptedException {
      // TODO Auto-generated method stub
      System.setProperty("webdriver.chrome.driver", "C:\Training\Automation\Software\drivers\chromedriver_win32\chromedriver.exe");
      WebDriver driver=new ChromeDriver();
      driver.get("https://www.hao123.com/");
      Thread.sleep(5000);
      
      driver.findElement(By.xpath("//*[@id='box-famoussite']/div/div[2]/span[2]/a")).click();
      Thread.sleep(5000);
      System.out.println("进入58同城");
      
    //  String[] handles=new String[driver.getWindowHandles().size()];
    //  driver.getWindowHandles().toArray(handles);
    //  driver.switchTo().window(handles[1]);
      for (String handle:driver.getWindowHandles())
      {
       driver.switchTo().window(handle);
      }
      Thread.sleep(1000);
      driver.findElement(By.xpath("//*[@id='nav']/li[7]/a")).click();
      Thread.sleep(5000);
      System.out.println("进入安居客");
      
    //  String[] handles1=new String[driver.getWindowHandles().size()];
    //  driver.getWindowHandles().toArray(handles1);
    //  driver.switchTo().window(handles1[2]);
      for (String handle:driver.getWindowHandles())
      {
       driver.switchTo().window(handle);
      }
      driver.findElement(By.xpath("//*[@id='header']/div[3]/div[2]/a[1]")).click();
      Thread.sleep(5000);
      System.out.println("进入我要推广");
      for (String handle:driver.getWindowHandles())
      {
       driver.switchTo().window(handle);
      }
      driver.findElement(By.xpath("//*[@id='footer']/ul[1]/li[11]/a")).click();
      Thread.sleep(5000);
      System.out.println("进入房贷计算器");
      
      String[] handles=new String[driver.getWindowHandles().size()];
      driver.getWindowHandles().toArray(handles);
      driver.switchTo().window(handles[2]);
      
      Thread.sleep(1000);
      driver.findElement(By.xpath("//*[@id='search-esf']")).sendKeys("上海浦东新区张江高科");
      System.out.println("回到第三个窗口进行操作");
      Thread.sleep(2000);
      
      Actions action=new Actions(driver);
    //  action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).perform();
      WebElement element=driver.findElement(By.xpath("//*[@id='search-esf']"));
      action.click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
      action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
      action.click(element).keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();
      System.out.println("在输入框里Ctrl a,Ctrl c,Ctrl v");
      
     }
    }

    下面是却换到单个页面的前后页面,即:页面的前进和后退

    driver.navigate().back();
    点击当前页面后退按钮
    driver.navigate().forward();
    点击当前页面的前进按钮
    driver.navigate().refresh();
    点击当前页面的刷新按钮

    除外---------------------------------------------------------------------------------------------------

    多窗体的情况使用driver.switchTo().window()
    这是一个Sample:
    Set<String> handles = driver.getWindowHandles();
    handles.remove(driver.getWindowHandle());//去掉当前句柄
    driver.switchTo().window(handles.iterator().next());
  • 相关阅读:
    洞察僵尸网络的4条关键线索,你知道吗?
    数据即服务(DaaS)的好处和趋势
    AIOT:什么是智联网,它是未来吗?
    渐变略过效果
    页面头部banner动画效果
    小三角
    监测屏幕宽度
    开关效果
    高级轮播
    手机端跳转页面指定楼层
  • 原文地址:https://www.cnblogs.com/111testing/p/6293092.html
Copyright © 2020-2023  润新知