安裝並配置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();
}
}