1.使用intellij新建一个maven项目,名字自定。在pom中写入selenium的依赖。其他依赖也添加到该文件中。
[maven selenium依赖](http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/3.12.0)
[testNg依赖](http://mvnrepository.com/artifact/org.testng/testng/6.14.3)
1 23 7 8 9 10org.seleniumhq.selenium 4selenium-java 53.12.0 611 org.testng 12testng 136.14.3 14test 15
上述操作后,如果project报错,提示“the type's content type is element-only”,就将该行删除,重新输一次。
2.如果下载过慢,就修改settings。intellij-preferances-maven-usersetting file-settings.xml。通过目录查找settings.xml
alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central
3.简单的用例
import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import java.util.concurrent.TimeUnit;public class Baidu { public static void main(String[] args){ //设置Chromedriver的版本 //System.setProperty("webdriver.chrome.driver","路径"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); driver.get("https://www.baidu.com"); driver.findElement(By.xpath("//input[@class='s_ipt' and @id='kw']")).sendKeys("selenium"); driver.findElement(By.xpath("//input[@id='su']")).click(); System.out.println(driver.findElement(By.xpath("//span[@class='nums_text']")).getText()); driver.quit(); }}