• Java+Selenium 常见问题QA


    一:日期控件
    二:上传控件
    三:页面隐藏元素
    四:弹出窗口的处理
    五:富文本框编辑器
    六:元素高亮

    一:日期控件
    selenium不能直接对日期控件操作,可以通过js对日期控件做赋值操作

    WebElement inputTimeBox=driver.findElement(by.name("###"));
    //定位日期控件
    String time = "2015/10/10";
    ((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]",inputTimeBox, time);
    //通过JS给日期控件赋值

    此方法比较简便,但因为是给日期控件直接赋值,无法验证日期控件是否正常

    二:上传控件

    selenium无法直接操作windows窗体,上传控件有属性(type="file")时,可直接sendkeys处理上传文件

    WebElement adFileUpload = driver.findElement(By.id(" ###"));
    // 定位上传控件
    String filePath = "C:\test\uploadfile \test.jpg";
    // 定义了一个本地文件的路径
    adFileUpload.sendKeys(filePath);
    // 为上传控件进行赋值,将需要上传的文件路径赋给控件

    当上传控件是flash时,分享两个解决方案:

    1、借助Autolt工具,识别上传控件

    详情可参考:https://ke.qq.com/course/140125#term_id=100157931
    2、模拟键盘操作
    思路:将上传文件的路径模拟键盘操作复制粘贴到文件名框中,然后再模拟键盘的回车事件,上传文件
    第1步:将文件路径获取到windows剪切板(string参数)

    public static void setClipboardData(String string) {
    StringSelection stringSelection = new StringSelection(string);
    Toolkit.getDefaultToolkit().getSystemClipboard()
    .setContents(stringSelection, null);
    }

    第2步:将要上传的文件的路径通过windows快捷键ctrl+v粘贴到文件名控件中

    String filePath = "E:\1.txt";
    //上传的文件路径
    setClipboardData(filePath);
    //将文件路径传到剪切板
    //下面通过robot模拟粘贴快捷键
    Robot robot = null;
    try {
    robot = new Robot();
    } catch (AWTException e1) {
    e1.printStackTrace();
    }
    //robot模拟键盘操作,模拟键盘操作有其它方法,类似

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    //同时按下CONTROL+V,将上传文件路径黏贴到文件名
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    // 释放CONTROL+V

    robot.keyPress(KeyEvent.VK_ENTER);
    //回车事件,相当于点击打开

    针对上传,下载,弹出警告窗口等,都可借鉴以上两种方法处理,当然方法还有很多。

    三:页面隐藏元素
    针对页面隐藏元素的”display: none” 的情况,selenium没法直接操作元素,需要通过js将元素的none值改为'block':

    JavascriptExecutor js=(JavascriptExecutor)driver;
    js.executeScript("document.getElementById('li').style.display='block';"); //通过js把隐藏元素显示出来

    四:弹出窗口的处理
    public void switchToWindow(String windowTitle){

    for (int a = 0; a < =9; a++) {
    Set windowHandles = driver.getWindowHandles();
    //获得所有窗口的句柄
    for (String handler : windowHandles) {
    driver.switchTo().window(handler);
    String title = driver.getTitle();
    if (windowTitle.equals(title)) { //windowTitle: 目标窗口的title属性的实际值

    a = 10;
    break;
    }
    }
    }

    }

    for (int a = 0; a < =9; a++)这个循环的作用是提高稳定性,个别浏览器情况下一次遍历不到,会跳转失败

    五:富文本框编辑器
    driver.switchTo().frame("frame"); // 定位到富文本输入框所在的 frame
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.body.innerHTML='ABCDEFG'"); // 通过 js对富文本框赋值

    六:鼠标双击事件
    Actions action = new Actions(driver);
    action.doubleClick(driver.findElement(by.id(####))).perform(); //鼠标停留在当前位置做双击事件

    很多人在模拟双击事件是不成功,是因为没有在后面追加perform()

    同理,模拟鼠标悬浮的操作
    action.moveToElement(driver.findElement(by.id(####))).perform();

    七:元素高亮
    有时候在操作元素时操作失败,原因:1、定位错误 2、元素不可操作 如果无法确定失败原因,可以先用元素高亮的方法判断是否定位到该元素,在判断失败的原因

    publicvoid highlightElement(WebDriver driver,WebElement element) {
    JavascriptExecutor js =(JavascriptExecutor) driver;
    js.executeScript("element = arguments[0];" +
    "original_style =element.getAttribute('style');" +
    "element.setAttribute('style',original_style + ";" +
    "background: yellow; border: 2px solidred;");" +
    "setTimeout(function(){element.setAttribute('style',original_style);}, 1000);", element);
    } //封装一个方法

    highlightElement(driver,element) //调用,element是需要定位的目标元素

  • 相关阅读:
    springCloud中的服务调用feign
    模块化理解
    springCloud中的注册中心Nacos
    vue路由理解
    vue生命周期理解
    webpack理解
    连接查询理解
    [HDU]5691Sitting in Line (状压DP)
    [Kattis]redblacktree(树形依赖背包,DP优化)
    Codeforces Round #699 (Div. 2)
  • 原文地址:https://www.cnblogs.com/Shanghai-vame/p/7444211.html
Copyright © 2020-2023  润新知