1.根据谷歌版本下载chromedriver
http://chromedriver.storage.googleapis.com/index.html
2.IDEA新建maven项目
引入jar包:
maven仓库地址:https://mvnrepository.com/
搜索selenium-java、testng
pom.xml加入:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.4.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.6.1</version> <scope>test</scope> </dependency>
Test.java:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Test { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "E:/javaSoft/selenium/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize();//全屏 driver.get("http://localhost:8080/XXX/");//打开网址 driver.findElement(By.name("username")).sendKeys("000231"); driver.findElement(By.name("password")).sendKeys("11"); Thread.sleep(1000); //driver.findElement(By.name("password")).sendKeys(Keys.ENTER);//通过Enter登录 driver.findElement(By.id("loginForm")).submit();//通过提交form表单登录 } }