zoukankan      html  css  js  c++  java
  • 面试常见的selenium问题

    1.如何切换iframe

      问题:如果你在一个default content中查找一个在iframe中的元素,那肯定是找不到的。反之你在一个iframe中查找另一个iframe元素或default content中的元素,那必然也定位不到

      基本步骤:先通过switch进入到iframe中,找到想找的元素,然后跳出来,进行其他的操作

      1).定位到iframe:WebElement IframeElement=driver.findElement(By.id(“frame”));

      2).切到这个iframe 里面:Driver.switch().frame(IframeElement);

      3).定位Iframe里面,你想要的元素:

      WebElement content=driver.findElement(By.className("CSS1Compat"));//在iframe中定位要找的元素

      content.sendKeys("cke_contents_content");//操作元素

      driver.switchTo().defaultContent();//跳出iframe,不跳出来是不能进行iframe外的操作的

    2.如何处理弹窗

      1)处理弹窗就是一行代码:driver.switchTo().alert().accept(),这个弹窗就关闭了;

      2)alert()方法知识:http://www.w3school.com.cn/jsref/met_win_alert.asp

    3.如何处理上传文件

      注:selenium不能处理windows窗口,它能提供的方法就是,把图片或者文件的地址用sendkeys传给【上传文件/图片】控件,对于含有input element的上传, 我们可以直接通过sendkeys来传入文件路径

      1)找到上传控件element,并输入路径:

      WebElement element = driver.findElement(By.id("cloudFax-attachment-form-upload-input"));

      element.sendKeys(getFilePath(text.txt));

      2)路径的处理:

      private String getFilePath(String resource) {

      URL path = this.getClass().getResource(resource);

      return path.toString().replaceAll("file:/","");
      }

      附:看到另外一种简单粗暴的处理方法,只需要3条代码来处理此问题  

      WebElement uploadButton = driver.findElement(By.name("image"));

      String file="C:\Users\Public\Pictures\Sample Pictures\flower.jpg";

      uploadButton.sendKeys(file);

    相关链接:https://github.com/zhaohuif/-/wiki/Selenium-webdriver%E5%AD%A6%E4%B9%A0%E8%BF%87%E7%A8%8B%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98

         http://www.51testing.com/html/55/n-860455.html

           http://ask.testfan.cn/article/26

    4.如何切换浏览器窗口

      原理:webdriver是根据句柄来识别窗口的,因为句柄可以看做是窗口的唯一标识id。获取新窗口的思路是:先获取当前窗口句柄,然后获取所有窗口的句柄,通过排除当前句柄,来确定新窗口的句柄。获取到新窗口句柄后,通过switchto.window(newwindow_handle)方法,将新窗口的句柄当参数传入就可以捕获到新窗口了。

     //得到当前窗口的句柄 

      String currentWindow = dr.getWindowHandle();
      //得到所有窗口的句柄
      Set<String> handles = dr.getWindowHandles();
      //排除当前窗口的句柄,则剩下是新窗口(/*把Set集合转换成Iterator*/)
      Iterator<String> it = handles.iterator();//迭代器
      while(it.hasNext()){
      String handle = it.next();
      if(currentWindow.equals(handle)) continue;
      driver.close();
      WebDriver window = dr.switchTo().window(handle);
      System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());

    相关链接:http://jarvi.iteye.com/blog/1450626

         http://m.blog.csdn.net/article/details?id=8102135

  • 相关阅读:
    java8
    Python isinstance()
    Python3 File next()
    np.array() 与 np.mat() 比较
    np.nonzero()
    np.dot()
    算法之归并排序的应用 —— 小和问题以及逆序对问题
    递归过程以及递归master公式
    算法之异或运算及其应用
    算法之二分法及其应用
  • 原文地址:https://www.cnblogs.com/chenshaoping/p/5538536.html
Copyright © 2011-2022 走看看