//通过idWebElement element = driver.findElement(By.id("coolestWidgetEvah")); //通过classNameList cheeses = driver.findElements(By.className("cheese")); //通过tagNameWebElement frame = driver.findElement(By.tagName("iframe")); //通过nameWebElement cheese = driver.findElement(By.name("cheese")); //通过linkText cheeseWebElement cheese = driver.findElement(By.linkText("cheese")); //通过部分linkText search for cheeseWebElement cheese = driver.findElement(By.partialLinkText("cheese")); //通过css WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged")); //通过xpath,比较麻烦 //通过javaScript
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
List labels = driver.findElements(By.tagName("label")); List inputs = (List ) ((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);