//通过id
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
//通过className
List<WebElement> cheeses = driver.findElements(By.className("cheese"));
//通过tagName
WebElement frame = driver.findElement(By.tagName("iframe"));
//通过name
WebElement cheese = driver.findElement(By.name("cheese"));
//通过linkText
<a href="http://www.google.com/search?q=cheese">cheese</a>
WebElement cheese = driver.findElement(By.linkText("cheese"));
//通过部分linkText
<a href="http://www.google.com/search?q=cheese">search for cheese</a>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
//通过css
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
//通过xpath,比较麻烦
//通过javaScript
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
List<WebElement> labels = driver.findElements(By.tagName("label"));
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);