且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[Java]通过Selenium操作FireFox打开网页

更新时间:2022-05-22 21:20:48

先把maven依赖加上

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

思路代码:

WebDriver driver = FirefoxUtils.getFirefoxDriver();
 driver.get("网址");
  driver.close();//关闭驱动

FirefoxUtils类:

public class FirefoxUtils {
    public static FirefoxDriver getFirefoxDriver(String path) {
        File pathToBinary = new File(path);
        FirefoxBinary firefoxBinary = new FirefoxBinary(pathToBinary);
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");//firefox需要geckodriver驱动,这个,百度找咯
        return new FirefoxDriver(firefoxBinary, firefoxProfile);
    }

    /**
     * 设置默认Firefox路径,返回FirefoxDriver
     *
     * @return FirefoxDriver
     */
    public static FirefoxDriver getFirefoxDriver() {
        return FirefoxUtils.getFirefoxDriver("D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");//这里就是自己装的火狐路径呢
    }
}

selenium的话,我是用来操作网页进行自动化相关操作的,selenium的文档的话,我会上传到CSDN资源里面。这篇博文比较简单,具体对项目要求就结合文档,或者互联网。