selenium使用入门

/ 后端 / 没有评论 / 261浏览

我这里全部以Chrome浏览器(版本108.0.5359.125),jdk1.8为演示

1.下载浏览器相关驱动

根据对应版本下载驱动即可,我这个版本的chrome驱动: 108.0.5359.125驱动

2.添加maven依赖:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.7.2</version>
        </dependency>

如果是springboot项目,则可能出现版本错误问题,因为springboot已经管理了selenium-java版本;解决方式是显式的指定版本号:

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <selenium.version>4.7.2</selenium.version>
    </properties>

3.入门代码(访问百度并搜索)

package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;

public class Test1 {
    static {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver123.exe");
        // 关闭 Chrome 浏览器驱动的日志输出
        System.setProperty("webdriver.chrome.silentOutput", "true");
    }


    public static void main(String[] args) {
        baiduTest();
    }

    private static void baiduTest() {
        WebDriver driver = null;
        try {
            ChromeOptions option = new ChromeOptions();
            //读取chrome信息,含有登录信息等
            option.addArguments("--user-data-dir=C:/Users/78517/AppData/Local/Google/Chrome/User Data");
            //去除web driver痕迹(https://bot.sannysoft.com/测试)
            option.addArguments("disable-blink-features=AutomationControlled");
        
            driver = new ChromeDriver(option);
            driver.get("https://www.baidu.com");
            //隐式的进行超时等待页面加载,也可以使用sleep强制睡眠等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

            //模拟搜索
            driver.findElement(By.id("kw")).sendKeys("Chrome");

            //同时可以使用xpath语法,非常强大简单,语法为:定位(/或//)标签名[@元素名='属性值'],/为绝对定位,//为相对定位
            //driver.findElement(By.id("su")).click();
             driver.findElement(By.xpath("//input[@id='su']")).click();
            Thread.sleep(10000000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (driver != null) {
                // 关闭浏览器当前窗口
                driver.close();
                // 退出 webdriver 并关闭浏览器
                driver.quit();
            }
        }
    }

其他一些操作

    /**
     * 0.打开新标签和切换标签
     * 打开新标签页
     * driver.switchTo().newWindow(WindowType.TAB).get("https://www.v2ex.com");
     * 切换标签
     * List<String> windowHandlesList = new ArrayList<>(driver.getWindowHandles());
     * driver.switchTo().window(windowHandlesList.get(索引下标));
     * 1. 鼠标左键点击
     * Actions action = new Actions(driver);
     * action.click();   //鼠标左键点击当前停留的位置
     * action.click(driver.findElement(By.id(“kw”))); //鼠标左键点击指定的元素对象
     * <p>
     * 2. 鼠标右键点击
     * Actions action = new Actions(driver);
     * action.contextClick();   //鼠标右键点击当前停留的位置
     * action.contextClick(driver.findElement(By.id(“kw”))); //鼠标右键点击指定的元素对象
     * <p>
     * 3. 鼠标双击
     * Actions action = new Actions(driver);
     * action.doubleClick();  //鼠标双击当前停留的位置
     * action.doubleClick(driver.findElement(By.id(“kw”)));  //鼠标双击指定的元素对象
     * <p>
     * 4. 鼠标拖拽
     * Actions action = new Actions(driver);
     * action.dragAndDrop(el1,el2);  //鼠标将el1元素拖放到el2元素的位置
     * action.dragAndDrop(el1,x,y);  //鼠标el1元素拖放到(x, y)位置,x为横坐标,y为纵坐标
     * <p>
     * 5. 鼠标悬停
     * Actions action = new Actions(driver);
     * action.clickAndHold(el);  //鼠标悬停在el元素的位置
     * <p>
     * 6. 鼠标移动
     * Actions action = new Actions(driver);
     * action.moveToElement(el);  //将鼠标移到el元素
     * action.moveToElement(el,x,y);   //将鼠标移到元素el的 (x, y) 位置
     * <p>
     * 7. 鼠标释放
     * action.release();  //释放鼠标
     */