Java+Selenium3方法篇2

1.多窗口切换

 public static void window_swtich(WebDriver driver) {
        /**
         * @description:
         * @param: [driver]
         * @return: void
         * @author: ljx
         * @time: 2020/6/4 16:13
         */
        //多窗口切换
        String handler = driver.getWindowHandle();
        System.out.println(handler);
        Set handlers = driver.getWindowHandles();
        for (String s : handlers) {
            if (handler.equals(s)) {
//                driver.close();
                continue;
            }
            driver.switchTo().window(s);
        }

    }

2.执行js语句

 public static void execute_js(WebDriver d, WebElement element, String js_str) {
      /**
       * @description:
       * @param d
       * @param element
       * @param js_str
       * @return: void
       * @author: ljx
       * @time: 2020/6/4 16:16
       */
        JavascriptExecutor js = (JavascriptExecutor) d;
        js.executeScript(js_str, element);

    }

3.切换frame

 public static void swtich_frame(WebDriver driver){
        /**
         * @description:
         * @param driver
         * @return: void
         * @author: ljx
         * @time: 2020/6/4 17:25
         */
        //可以填写int值,string,和webelement
        driver.switchTo().frame("rightMain");
    }

4.弹窗与元素是否显示是否被选中

element1.isSelected();//判断元素是否被选中
element1.isDisplayed();//判断元素是否显示
//单选和多选框操作都是定位到元素组,判断元素是否被选中.isSelected()
//处理alert弹窗
driver.switchTo().alert().accept();
driver.switchTo().alert().dismiss();
driver.switchTo().alert().getText();
driver.switchTo().alert().sendKeys("aaa");

5.通过js给要操作的元素设置颜色

System.setProperty("webdriver.gecko.driver", ".\\Tools\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.get("https://www.baidu.com/");

        Thread.sleep(2000);

        // 点击登录
        driver.findElement(By.xpath("//*[@id='u1']/a[2]")).click();
        Thread.sleep(500);
        driver.findElement(By.xpath("//p[@title='用户名登录']")).click();
        WebElement username = driver.findElement(By.id("TANGRAM__PSP_11__userName"));

        // 创建一个JavascriptExecutor对象
        JavascriptExecutor js = (JavascriptExecutor) driver;

        username.sendKeys("abcdefg");
        // 设置颜色
        js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", username);

        WebElement password = driver.findElement(By.id("TANGRAM__PSP_11__password"));
        js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", password);
        Thread.sleep(1000);

6.Actions悬停和选中文字

//移动到元素悬停Actions
        driver.get("https://www.baidu.com/");
        WebElement setting = driver.findElement(By.id("s-usersetting-top"));
        Actions actions = new Actions(driver);
        actions.moveToElement(setting).perform();
        driver.findElement(By.linkText("高级搜索")).click();

        //选取一段文字
        driver.get("https://www.baidu.com/duty/");

        //定义第一段文字
        WebElement Sting_Sected = driver.findElement(By.xpath("//*/div[@id='aboutToShow']//p"));

        //定义第二段文字
        WebElement String_Second = driver.findElement(By.xpath("//*/div[@id='aboutToShow']//ul/li[2]"));

        Actions action = new Actions(driver);
        action.clickAndHold(Sting_Sected).moveToElement(String_Second).perform();
        action.release();

7.截图

TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
File f = takesScreenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f,new File(".\\screntshots\\a.png"));

8.读取excel文件

//1..下载POI,打开网站http://poi.apache.org/download.html
        //2.引用jar包
        //3.使用poi进行读写excel文件
        //文件位置
        File f = new File(".\\Files\\test.xlsx");
        FileInputStream fileInputStream = new FileInputStream(f);
        @SuppressWarnings("resource")
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
//        System.out.println(sheet.getRow(0).getCell(0).getStringCellValue());
        //遍历获取表格内容
        for(int i=0;i

9.读取properties文件

        //读取properties文件
        FileInputStream inputStream = new FileInputStream(".\\Files\\config.properties");
        Properties p = new Properties();
        p.load(inputStream);
        System.out.println(p.getProperty("server"));

10.获取现在日期

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("YYYY:MM:dd-HH:mm:ss");
        String time = format.format(date);
        System.out.println(time);

 

你可能感兴趣的:(Java+Selenium3方法篇2)