Selenium API

    //執(zhí)行js
    public static void executeJs()
    {
        //法一
        JavascriptExecutor js1 = (JavascriptExecutor)driver;
        //1.通過js獲取元素
        WebElement username = (WebElement)js1.executeScript("document.getElementById(\"j_username\")");
        //2.輸入用戶名
        username.clear();
        username.sendKeys("username");
        
        //法二
        //1.獲取元素
        WebElement searchButton = driver.findElement(By.id("search"));
        //2.js
        JavascriptExecutor js = (JavascriptExecutor)driver;
        String jsStr = "arguments[0].click();";
        //執(zhí)行js
        js.executeScript(jsStr, searchButton);
    }
    //截屏操作
    public static void screenShot()
    {
//        OutputStream out = null;
        //1.new截屏
        TakesScreenshot take = (TakesScreenshot)driver;
        //2.截出的圖片|原始圖片位置
        File file = take.getScreenshotAs(OutputType.FILE);
        //C:\Users\Zhou\AppData\Local\Temp
        
        //3..截屏的目標(biāo)文件位置
        String desPath = "D:/Selenium Auto/Demo/screenshot.jpg";
        File destFile = new File(desPath);
        //4.刪除目標(biāo)文件位置的圖片|路徑為文件且不為空則進(jìn)行刪除 
        if (destFile.isFile()||destFile.exists()) {  
            destFile.delete();  
        } 
   
        try {
//          out = new FileOutputStream(desPath);
//          FileUtils.copyFile(file, out);
            //5.移動到目標(biāo)位置
            FileUtils.moveFile(file, destFile);
            //6.刪除默認(rèn)存儲的原始位置圖片
            file.delete();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
//雙擊操作
    public static void doubleClick()
    {
        //1.
        Actions act = new Actions(driver); 
        //2.找到登錄按鈕
        WebElement submit = driver.findElement(By.id("su"));
//      driver.findElement(By.id("kw")).sendKeys("selenium");
        //3.雙擊
//      act.doubleClick(submit).build().perform();
        act.doubleClick(submit).perform();
    }
    //拖拽操作
    public static void drag()
    {
        //1.獲取元素
        WebElement src = driver.findElement(By.id("drag"));
        WebElement target = driver.findElement(By.id("drop"));
        //2.拖拽
        Actions act = new Actions(driver);
        act.dragAndDrop(src, target);
    }
    //下拉列表
    public static void select()
    {
        //1.獲取元素
        WebElement dropList = driver.findElement(By.id("dropList"));
        //2.轉(zhuǎn)為下拉框
        Select select = new Select(dropList);
        //獲取select里的所有option
//      List<WebElement> opts = select.getOptions();
//      Iterator<WebElement> iter = opts.iterator();

        //3.選中第一個
        select.selectByIndex(0);
        select.selectByValue("選中第一個option");
        select.selectByVisibleText("選中第一個option");
    }
    //單選按鈕,復(fù)選框操作
    public static void radioOrCheckBox()
    {
        //1.獲取元素
        WebElement radioOrCheckBox = driver.findElement(By.cssSelector("form input:nth-child(1)"));
        //2.點(diǎn)擊
        radioOrCheckBox.click();
        //判斷是否轉(zhuǎn)中
        radioOrCheckBox.isSelected();
    }

Selenium API

package com.selenium;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class apiTest {

    public static WebDriver driver;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        driver = new FirefoxDriver();
        System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");

        String url = "http://baidu.com";
        driver.get(url);
        
        //執(zhí)行js
//      executeJs();
        
        //截屏
//      screenShot();
        
        //雙擊
//      doubleClick();
        
        //拖拽
//      drag();
        
        //下拉列表
//      select();
        
        //單選按鈕,復(fù)選框操作
//      radioOrCheckBox();
    }
    
    //執(zhí)行js
    public static void executeJs()
    {
        //法一
        JavascriptExecutor js1 = (JavascriptExecutor)driver;
        //1.通過js獲取元素
        WebElement username = (WebElement)js1.executeScript("document.getElementById(\"j_username\")");
        //2.輸入用戶名
        username.clear();
        username.sendKeys("username");
        
        //法二
        //1.獲取元素
        WebElement searchButton = driver.findElement(By.id("search"));
        //2.js
        JavascriptExecutor js = (JavascriptExecutor)driver;
        String jsStr = "arguments[0].click();";
        //執(zhí)行js
        js.executeScript(jsStr, searchButton);
    }
    
    //下拉列表
    public static void select()
    {
        //1.獲取元素
        WebElement dropList = driver.findElement(By.id("dropList"));
        //2.轉(zhuǎn)為下拉框
        Select select = new Select(dropList);
        //獲取select里的所有option
//      List<WebElement> opts = select.getOptions();
//      Iterator<WebElement> iter = opts.iterator();

        //3.選中第一個
        select.selectByIndex(0);
        select.selectByValue("選中第一個option");
        select.selectByVisibleText("選中第一個option");
    }
    
    //單選按鈕,復(fù)選框操作
    public static void radioOrCheckBox()
    {
        //1.獲取元素
        WebElement radioOrCheckBox = driver.findElement(By.cssSelector("form input:nth-child(1)"));
        //2.點(diǎn)擊
        radioOrCheckBox.click();
        //判斷是否轉(zhuǎn)中
        radioOrCheckBox.isSelected();
    }
    
    //拖拽操作
    public static void drag()
    {
        //1.獲取元素
        WebElement src = driver.findElement(By.id("drag"));
        WebElement target = driver.findElement(By.id("drop"));
        //2.拖拽
        Actions act = new Actions(driver);
        act.dragAndDrop(src, target);
    }
    
    //雙擊操作
    public static void doubleClick()
    {
        //1.
        Actions act = new Actions(driver); 
        //2.找到登錄按鈕
        WebElement submit = driver.findElement(By.id("su"));
//      driver.findElement(By.id("kw")).sendKeys("selenium");
        //3.雙擊
//      act.doubleClick(submit).build().perform();
        act.doubleClick(submit).perform();
    }
    
    //截屏操作
    public static void screenShot()
    {
//        OutputStream out = null;
        //1.new截屏
        TakesScreenshot take = (TakesScreenshot)driver;
        //2.截出的圖片|原始圖片位置
        File file = take.getScreenshotAs(OutputType.FILE);
        //C:\Users\Zhou\AppData\Local\Temp
        
        //3..截屏的目標(biāo)文件位置
        String desPath = "D:/Selenium Auto/Demo/screenshot.jpg";
        File destFile = new File(desPath);
        //4.刪除目標(biāo)文件位置的圖片|路徑為文件且不為空則進(jìn)行刪除 
        if (destFile.isFile()||destFile.exists()) {  
            destFile.delete();  
        } 
   
        try {
//          out = new FileOutputStream(desPath);
//          FileUtils.copyFile(file, out);
            //5.移動到目標(biāo)位置
            FileUtils.moveFile(file, destFile);
            //6.刪除默認(rèn)存儲的原始位置圖片
            file.delete();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void api(){
        
        // 創(chuàng)建了一個 Firefox driver 的實(shí)例
        // 注意,其余的代碼依賴于接口而非實(shí)例
        driver = new FirefoxDriver();
        System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
        
        // 使用它訪問 Google
        driver.get("http://www.google.com");
        // 同樣的事情也可以通過以下代碼完成
        // driver.navigate().to("http://www.google.com");

        // 找到搜索輸入框
        WebElement element = driver.findElement(By.name("q"));

        // 輸入要查找的詞
        element.sendKeys("Cheese!");

        // 提交表單
        element.submit();

        // 檢查頁面標(biāo)題
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google 搜索結(jié)果由 JavaScript 動態(tài)渲染
        // 等待頁面加載完畢,超時時間設(shè)為10秒
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        //應(yīng)該能看到: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //關(guān)閉瀏覽器
        driver.quit();
    }
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容