爱玺玺

爱玺玺的生活日记本。wx:lb87626

java连接谷歌浏览器

package test;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


public class Test7 {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe"); // 请设置 chromedriver 的实际路径

        WebDriver driver = new ChromeDriver();


        String url = "https://www.igengmei.com/";

        driver.get(url);

        Thread.sleep(3000); // 等待页面加载完成,可适当调整等待时间


        WebElement elem = driver.findElement(By.tagName("body"));

        String html = elem.getAttribute("innerHTML");

        System.out.println(html);


        //driver.quit();

    }

}



https://chromedriver.chromium.org/downloads  这里下载chromedirver.exe和浏览器版本号对应,放在浏览器同一个目录里面。

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5