java+selenium-3.9.1多線程 打開連接截取屏幕截圖


廢話不多說上代碼:(我是用的chrome舉得例子哈)

第一步,需要chromedriver.exe 目的和調起chrome 瀏覽器打開連接,chromedriver.exe的版本與你的chrome版本一致才行,放一張版本對照圖

下載chromedriver連接http://chromedriver.storage.googleapis.com/index.html

 

第二步:上代碼

package src;
import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebDriverUtil implements Runnable {
      public final String ROOT_PATH           = System.getProperty("user.dir"); // 獲取工作目錄路徑
    public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
    public WebDriver driver                 = null;

    // 打開URL連接
     public void open() {
        // System.setProperty("webdriver.chrome.driver",this.ROOT_PATH+"/dirver/chromedriver.exe");//指定驅動路徑
        // 選擇執行對象,版本信息,以及系統類型
        DesiredCapabilities ffDesiredcap= new DesiredCapabilities("chrome", "75.0.3770.142", Platform.WINDOWS);
        try{
            this.driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),ffDesiredcap);
            this.driver.get(path);
            this.driver.manage().window().maximize();
        } catch (Exception e) {
            System.out.println(Thread.currentThread().getId()+" 錯誤 !!!");
            e.printStackTrace();
        }
    }

/**
     * 截取屏幕
     * @param fileName
     */
    public void captureScreenshot() {
        String dirName = "e:/test";
        if (!(new File(dirName).isDirectory())) {
            new File(dirName).mkdir();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
     
        String date = sdf.format(new Date());
        TakesScreenshot tsDriver = (TakesScreenshot) this.driver;
        File saveImage = new File(dirName + File.separator + date + "_" + fileName==null?"":fileName + ".png");
        tsDriver.getScreenshotAs(OutputType.FILE).renameTo(saveImage);
    }
}

首先上面這個代碼是一個單獨的類

想使用多線程並發打開連接,就得在多線程,每個線程內去進行以下操作

WebDriverUtil webDriverUtil = new WebDriverUtil();
webDriverUtil.open("file:///E:\\css3_button33\\index.html");
webDriverUtil.captureScreenshot("as");

注意:必須在多線程每個線程內去實例化調用



還沒完,解釋一下:

http://localhost:4444/wd/hub 是固定的url,服務端url,
ffDesiredcap 是執行瀏覽器對象
 RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),ffDesiredcap)


需要服務端去監聽4444 端口,執行selenium-server-standalone-3.9.1.jar程序

鏈接:https://pan.baidu.com/s/1j7sKkm2FlNumYVjWSBtbCw   去下載 selenium-server-standalone-3.9.1.jar

提取碼:q8oi

 

下載下來 執行, selenium-server-standalone-3.9.1.jar 當前所在的目錄下執行這個jar 命令下面執行

java -Dwebdriver.chrome.driver="E:\test\chromedriver.exe" -jar selenium-server-standalone-3.9.1.jar
執行完了之后,為了檢測是否監聽了4444端口后可以成功調用chrome瀏覽器,打開鏈接 http://localhost:4444/wd/hub/static/resource/hub.html

 

 點擊ok,會打開chrome瀏覽器一個窗口,恭喜你成功了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM