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 <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> 2 <dependency> 3 <groupId>org.seleniumhq.selenium</groupId> 4 <artifactId>selenium-java</artifactId> 5 <version>3.12.0</version> 6 </dependency> 7 8 9 <!-- https://mvnrepository.com/artifact/org.testng/testng --> 10 <dependency> 11 <groupId>org.testng</groupId> 12 <artifactId>testng</artifactId> 13 <version>6.14.3</version> 14 <scope>test</scope> 15 </dependency>
上述操作后,如果project报错,提示“the type's content type is element-only”,就将该行删除,重新输一次。
2.如果下载过慢,就修改settings。intellij-preferances-maven-usersetting file-settings.xml。通过目录查找settings.xml
<mirror> <id>alimaven</id> <name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOF> </mirror>
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(); } }