zoukankan      html  css  js  c++  java
  • selenium测试(Java)--执行JS(十八)

    1.  操作滚动条

    package com.test.js;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class WindowScroll {
    
        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.baidu.com");
            driver.manage().window().setSize(new Dimension(600, 600));
    
            waitTime(3000);
            driver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
            driver.findElement(By.cssSelector("#su")).click();
    
            waitTime(3000);
            String js = "window.scrollTo(100,450);";
            ((JavascriptExecutor) driver).executeScript(js);
    
            waitTime(5000);
            driver.quit();
    
        }
    
        static public void waitTime(int time) {
    
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    2.在textarea中输入内容

    package com.test.js;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class TextareaInput {
    
        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/js/textarea.html");
            driver.manage().window().maximize();
    
            driver.findElement(By.cssSelector("#id")).sendKeys("input text----");
    
            // 利用JS来输入内容
            waitTime(5000);
            String text = "input by js";
            String js = "var sum = document.getElementById('id'); sum.value='" + text + "';";
            System.out.println(js);
            ((JavascriptExecutor) driver).executeScript(js);
    
        }
    
        static public void waitTime(int time) {
    
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    参考:

    http://www.cnblogs.com/tobecrazy/p/4817946.html

  • 相关阅读:
    PhpStorm函数注释的设置
    thinkphp5 返回数组提示variable type error: array
    js获取json对象中的key和value,并组成新数组
    PHP生成随机字符串与唯一字符串
    yii2-admin扩展自定义目录
    PHP7.3发布啦
    服务器环境从PHP5升级到PHP7
    亲测能用的mysqli类,挺好用的
    PHP必用代码片段
    git flow的使用
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/7289589.html
Copyright © 2011-2022 走看看