zoukankan      html  css  js  c++  java
  • selenium 隐藏浏览器+截图

    python代码如下

    from selenium import webdriver
    #隐藏浏览器
    chrome_opts=webdriver.ChromeOptions()
    chrome_opts.add_argument("--headless")
    #Chromedriver存放路径
    path='C:\Users\yjh\Downloads\chromedriver_win32\chromedriver.exe'
    driver=webdriver.Chrome(path,chrome_options=chrome_opts)
    try:
        driver.get("http://www.baidu.com")
        driver.find_element_by_id('kw').send_keys('12306')
        #截图
        driver.get_screenshot_as_file("D:\baidu12306.png")
    except RuntimeError as error:
        print(error)
    finally:
        driver.quit()

     java代码如下,需要添加依赖

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import java.io.File;
    import java.io.IOException;
    
    public class ScreenShot {
        public static void main(String[] args) {
            //设置启动路径
            System.setProperty("webdriver.chrome.driver","C:\Users\yjh\Downloads\chromedriver_win32\chromedriver.exe");
            //隐藏浏览器
            ChromeOptions options=new ChromeOptions();
            options.addArguments("--headless");
    
            WebDriver driver=new ChromeDriver(options);
            driver.get("url");
            //截图
            File scr=((ChromeDriver) driver).getScreenshotAs(OutputType.FILE);
            try {
                //拷问截图文件
                FileUtils.copyFile(scr,new File("D:\screenShot.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            driver.quit();
        }
    }
  • 相关阅读:
    整合Spring与Hibernate
    基本正则
    vue权限指令
    vue数字动态转换大写
    element ui 表格动态生成多级表头、一级表头,可无限嵌套
    vuex和vue-router全家桶
    element表格内容过多title提示
    HBuilder打包App方法
    mui底部选项卡切换页面
    mui框架的地步选项卡公用加载对应页面demo
  • 原文地址:https://www.cnblogs.com/yjh1995/p/11845079.html
Copyright © 2011-2022 走看看