当前位置:网站首页>Phantomjs Usage Summary

Phantomjs Usage Summary

2022-06-23 00:49:00 csdncjh

download

download | PhantomJS

maven rely on

      <!--phantomjsdriver-->
        <dependency>
            <groupId>com.codeborne</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.2.1</version>
        </dependency>

  Using encapsulation

PhantomJsUtils

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class PhantomJsUtils {
    private static DesiredCapabilities dcaps = new DesiredCapabilities();
    private static final String ua = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 baidu/3.2.1";
    private static boolean isLinux = false;

    static {

        String os = System.getProperty("os.name");
        String path = null;
        if (os.toLowerCase().contains("windows")) {
            path = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
            System.setProperty("webdriver.chrome.driver", path);
        } else {
            path = "./phantomjs/phantomjs";
            System.setProperty("webdriver.chrome.driver", path);
            isLinux = true;
        }

        //  Necessary parameters 
        System.setProperty("phantomjs.binary.path", path);
//        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl Certificate support 
        dcaps.setCapability("acceptSslCerts", true);
        // Screenshots support 
        dcaps.setCapability("takesScreenshot", true);

        //css Search support 
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js Support 
        dcaps.setJavascriptEnabled(true);
        // Drive support 
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                path);

        //ua
        dcaps.setCapability("phantomjs.page.settings.userAgent", ua);
        dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", ua);

        // Set connection timeout 
        System.setProperty("sun.net.client.defaultConnectTimeout", "50");
        System.setProperty("sun.net.client.defaultReadTimeout", "50");


        // Optimize 
        List<String> cli = new ArrayList<>();
        cli.add("--load-images=" + true);
//        cli.add("--output-encoding=" + "utf-8");
        cli.add("--disk-cache=yes");
        cli.add("--ignore-ssl-errors=true");
        cli.add("--debug=false");

        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli);
        // agent 
       /* String ip = "192.168.6.1:1984";
        //  Enable disk caching ( Cache storage location in Desktop Services , The default is false). Also accept :[yes|no].
        // --ignore-ssl-errors=[true|false] Ignore SSL error , For example, expired or self signed certificate error ( The default is false). Also accept :[yes|no].
        // --proxy=address:port Specify the proxy server to use ( for example --proxy=192.168.1.42:8080).
        String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + ip };
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);*/
    }

    public static PhantomJSDriver initDriver() {
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        // Set hidden wait ( Act on the whole )
        driver.manage().timeouts()
                .pageLoadTimeout(20, TimeUnit.SECONDS)
                .implicitlyWait(20, TimeUnit.SECONDS);
        return driver;
    }

    /**
     *  Using agents 
     * @param proxy ip:port
     * @return
     */
    public static PhantomJSDriver initDriver(String proxy) {
        DesiredCapabilities dcaps = new DesiredCapabilities();
        String os = System.getProperty("os.name");
        String path = null;
        if (os.toLowerCase().contains("windows")) {
            path = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
            System.setProperty("webdriver.chrome.driver", path);
        } else {
            path = "./phantomjs/phantomjs";
            System.setProperty("webdriver.chrome.driver", path);
            isLinux = true;
        }

        //  Necessary parameters 
        System.setProperty("phantomjs.binary.path", path);
//        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl Certificate support 
        dcaps.setCapability("acceptSslCerts", true);
        // Screenshots support 
        dcaps.setCapability("takesScreenshot", true);

        //css Search support 
        dcaps.setCapability("cssSelectorsEnabled", true);
        //js Support 
        dcaps.setJavascriptEnabled(true);
        // Drive support 
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                path);
        //ua
        dcaps.setCapability("phantomjs.page.settings.userAgent", ua);
        dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", ua);
        // Set connection timeout 
        System.setProperty("sun.net.client.defaultConnectTimeout", "50");
        System.setProperty("sun.net.client.defaultReadTimeout", "50");
        // Optimize 
        List<String> cli = new ArrayList<>();
        cli.add("--load-images=" + true);
//        cli.add("--output-encoding=" + "utf-8");
        cli.add("--disk-cache=yes");
        cli.add("--ignore-ssl-errors=true");
        cli.add("--debug=false");

        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli);
        // agent 
//        String ip = "192.168.6.1:1984";
        //  Enable disk caching ( Cache storage location in Desktop Services , The default is false). Also accept :[yes|no].
        // --ignore-ssl-errors=[true|false] Ignore SSL error , For example, expired or self signed certificate error ( The default is false). Also accept :[yes|no].
        // --proxy=address:port Specify the proxy server to use ( for example --proxy=192.168.1.42:8080).
        String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + proxy };
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);

        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        // Set hidden wait ( Act on the whole )
        driver.manage().timeouts()
                .pageLoadTimeout(50, TimeUnit.SECONDS)
                .implicitlyWait(50, TimeUnit.SECONDS);
        return driver;
    }


    public static void close(WebDriver driver) {
        if (driver != null) {
            try {
                driver.quit();
            } catch (Exception e) {
                System.out.println("close error");
            }
        }

    }

    /**
     *  Full page screenshot 
     *
     * @param driver
     * @param destPath
     * @throws IOException
     */
    public static void screenshot(WebDriver driver, String destPath) throws IOException {
        File dfile = new File(destPath);
        if(dfile.exists()){
            dfile.delete();
        }
        File outFile = ((PhantomJSDriver) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.moveFile(outFile, new File(destPath));

    }

    public static String getCookieStr(WebDriver driver) {
//        driver.navigate().refresh();
        String cookieStr = null;
        for (Cookie ck : driver.manage().getCookies()) {
            cookieStr += (ck.getName() + "=" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure() + ";");
//            cookieStr += ck.getName() + "=" + ck.getValue()+";";
        }

        return cookieStr.substring(0, cookieStr.length() - 1).replaceFirst("null", "");
    }


    public static void main(String[] args) {
        WebDriver driver = initDriver("192.168.6.1:1984");
        String url = "https://en.wikipedia.org/wiki/China";
        driver.get(url);
        System.out.println(driver.getPageSource());

        close(driver);

    }
}

source

phantomJs_Selenium_java Most fully configured access _ Vegetable chicken java Programmer's blog -CSDN Blog

Selenium+Phantomjs do Java Reptiles _ Tomato, tomato blog -CSDN Blog _java phantomjs selenium 

原网站

版权声明
本文为[csdncjh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221445519582.html