pom 文件添加一下代码
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit-cssparser</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit-core-js</artifactId>
<version>2.44.0</version>
</dependency>
下载 geckodriver下载地址
https://github.com/mozilla/geckodriver/releases
测试代码
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
/**
* javaURL 类。
*
* @author zjm
* @date 2022-07-28 [zjm] 创建。
*/
public class JavaURL {
private static final Logger log= LogManager.getLogger();
public static void main(String[] args) throws Exception{
// System.setProperty("webdriver.firefox.marionette","e:/tools/geckodriver.exe");
System.setProperty("webdriver.gecko.driver", "e:/tools/geckodriver.exe");
// WebDriver driver=new FirefoxDriver();
FirefoxOptions options = new FirefoxOptions();
WebDriver webDriver = new FirefoxDriver(options);
JavaURL.testBrowser(webDriver);
// JavaURL.crawlData();
}
public static void testBrowser(WebDriver driver) throws Exception
{
driver.get("https://www.baidu.com");
Thread.sleep(2000);
log.info(driver.getPageSource());
// 浏览器退出
driver.quit();
}
}
网友评论