安装并配置IntelliJ IDEA及Maven
具体教程网上已有很多,不再赘述哟~~~
新建Maven项目
步骤如下(其中包括几个自己遇到的坑,希望也可以帮助到大家):
- 点击Create New Project来创建新项目
- 选择maven项目
- 填写GroupId、ArtifactId、Version信息(此处仅为测试,起名不规范哈~~~)
- 修改Maven Home Dictionary为maven安装位置,User settings file为conf中的setting.xml
- 编辑project name后,点击Finish
- 完成Maven项目的创建
添加Selenium3的jar包
- 打开selenium的仓库地址 :http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
- 选择需要的版本,显示如下:
- 将maven标签中的内容复制到pom.xml中
注意:将复制的内容放入dependencies标签中
- 点击右下角提示框中的Enable Auto-Import,自动导入包
- 待包成功导入后,在右边的Maven中会显示通过maven导入的jar包
下载第三方驱动
selenium要依赖浏览器才能跑起来,所以必须要下载第三方驱动
【驱动链接参考博文:https://blog.csdn.net/weixin_40686603/article/details/84893595】
驱动资源:https://pan.baidu.com/s/1gXtMTHLuvLNR_Z6hCAHMkg
提取码:if2k
简单小demo-自动打开百度
- 在maven项目中的/src/main目录下新建一个java文件,若无法创建(如下图,没有新建java和package包选项),可进行后面操作
(1) File - Project Structure - Modules,选中java文件夹,然后点击Sources,若文件夹变为蓝色,则设置成功
- 在java文件夹下新建一个java.class文件
- demo代码如下:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
public class Demo {
public static void main(String[] args) {
File file = new File("D://intellij idea/chromedriver.exe");
System.setProperty("webdriver.chrome.driver",file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
String title = driver.getTitle();
System.out.printf(title);
//driver.close();
}
}