写个web自动化

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

import java.io.File;

public class Test{
    
    
    public static void main(String[] args) {
    
    

        // 加载相应的驱动,第二个参数是驱动路径
        System.setProperty("webdriver.chrome.driver","src/test/chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        // 放大窗口
        driver.manage().window().maximize();

        // 访问www.baidu.com
        driver.get("https://www.baidu.com");

        // 获取输入框,并输入hello Test
        driver.findElement(By.id("kw")).sendKeys("hello Test");

        // 点击”搜索“按钮
        driver.findElement(By.id("su")).click();
        try {
    
    
            Thread.sleep(3000);
        }catch (InterruptedException i){
    
    
            i.printStackTrace();
        };

        driver.close();
    }

}

写这个的时候要先在pom文件中加上

 <dependencies>

        <!-- selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>

让pom先加载,这个加载时间有点长,大概10分钟左右。

还有个就是要搞个驱动
https://pan.baidu.com/s/1n1xm90iy4Un5zFNooDiZsg 提取码:whyy
将这个驱动放进去后就可以了,原先我以为浏览器版本和这个驱动要对应,我还下载了对应的低版本的谷歌浏览器,发现高版本也可以用。我的谷歌版本
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41977380/article/details/112527612