selenium实战-1

selenium 简介

Selenium is a suite of tools to automate web browsers across many platforms.
runs in many browsers and operating systems
can be controlled by many programming languages and testing frameworks

官网:https://www.selenium.dev/

浏览器复用

//浏览器
chrome -remote-debugging-port=9222
//java
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
driver = new ChromeDriver(options);

cookie

Cookie 是一些数据, 存储于你电脑上的文本文件中
当 web 服务器向浏览器发送 web 页面时,在连接关闭后,服务端不会记录用户的信息
Cookie 解决“如何记录客户端的用户信息”

代码

package junit5;


import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;

public class TestSelenium {
    WebDriver driver;

    @Test
    public void testSelenium() {
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
        //利用chromedriver控制chrome
        driver = new ChromeDriver();
        driver.get("https://work.weixin.qq.com/wework_admin/frame");
        driver.get("https://work.weixin.qq.com/wework_admin/frame");
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
    public void loadCookie(WebDriver driver) {
        try {
            FileReader fileReader = new FileReader("cookie.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                StringTokenizer tokenizer = new StringTokenizer(line, ";");
                String name = tokenizer.nextToken();
                String value = tokenizer.nextToken();
                String domain = tokenizer.nextToken();
                String path = tokenizer.nextToken();
                Date expiry = null;
                String dt = tokenizer.nextToken();
                if (!dt.equals("null")) {
                    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
                    //把 string 转换成 date
                    expiry = sdf.parse(dt);
                }
                //把 string 转换成 boolean
                boolean isSecure = Boolean.parseBoolean(tokenizer.nextToken());
                Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure);
                driver.manage().addCookie(cookie);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveCookie(WebDriver driver) {
        //每次只取一条cookie进行处理
        try {
            FileWriter fileWriter = new FileWriter("cookie.txt");
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for (Cookie cookie : driver.manage().getCookies()) {
                bufferedWriter.write(cookie.getName() + ';' +
                        cookie.getValue() + ";" +
                        cookie.getDomain() + ";" +
                        cookie.getPath() + ";" +
                        cookie.getExpiry() + ";" +
                        cookie.isSecure());
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

老师,昨天上课代码放到GIT上了吗?分享下地址

1 Like

如果电脑是mac电脑:
需要将/Applications/Google\ Chrome.app/Contents/MacOS
和/Applications中的Google\ Chrome修改成Chrome,否则采用ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(“debuggerAddress”, “127.0.0.1:9222”);无法获取的浏览器的cookie,根源应该是ChromeOptions类中限定了名称为chrome:
public ChromeOptions() {
this.setCapability(“browserName”, “chrome”);
}

1 Like
关闭