通过id来获取元素
public class web_test02 { @Parameters(value = {"browserType"}) @Test public void test(String browserType) throws InterruptedException { System.out.println("浏览器为:"+browserType); //浏览器为:filefox // 指定浏览器驱动路径 System.setProperty ( "webdriver.chrome.driver", "driver\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); //访问百度 driver.get("http://www.baidu.com"); //输入123 driver.findElement(By.id("kw")).sendKeys("123"); //百度搜索一下 driver.findElement(By.id("su")).click(); //关闭浏览器 driver.quit(); } }
通过name属性来获取元素
package com.web_java01; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class web_test04 { @Test public void test(String browserType) throws InterruptedException { System.out.println("浏览器为:" + browserType); //浏览器为:filefox // 指定浏览器驱动路径 System.setProperty("webdriver.chrome.driver", "driver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //访问百度 driver.get("http://www.baidu.com"); //输入123 driver.findElement(By.name("wd")).sendKeys("123"); } }
通过tagname获取元素
//根据标签名 driver.findElement(By.tagName("input"));
通过css来定位
//css选择器 //有id选择器 # driver.findElement(By.cssSelector("#kw")).sendKeys("123"); //有样式选择器 . driver.findElement(By.cssSelector(".s_ipt")); //标签选择器 ,不需要任何符号 driver.findElement(By.cssSelector("input")).click();
原文:https://www.cnblogs.com/zhaobobo001/p/14655041.html