selenium-java之使用浏览器打开网页举例

依赖包:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

 

源码:

        String firefoxBrowserPath = "D:/Program Files/Mozilla Firefox/firefox.exe";
        String firefoxDriverPath = "D:/workspace/driver/geckodriver.exe";
        System.setProperty("webdriver.gecko.driver", firefoxDriverPath);
        System.setProperty("webdriver.firefox.bin",firefoxBrowserPath);

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        // 与浏览器同步非常重要,必须等待浏览器加载完毕
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        String url = "http://10.168.4.78:8085/?#/file-access";
        driver.get(url);
        Thread.sleep(2000);

        driver.quit();
        driver.close();

 

谷歌浏览器

System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver webDriver = new ChromeDriver();

 

浏览器驱动下载地址:

谷歌浏览器的驱动下载地址:https://chromedriver.storage.googleapis.com/index.html
火狐浏览器的驱动下载地址:https://github.com/mozilla/geckodriver/releases

 

posted @ 2020-09-28 16:35  星瑞  阅读(623)  评论(0编辑  收藏  举报