1: WebElement searchBox = driver.findElement(By.name("btnK"));
searchBox.click();
2:WebElement searchBox = driver.findElement(By.id("gbqfba"));
3: List<WebElement> buttons = driver.findElements(By.tagName("button"));
4:List<WebElement> allInputs = driver.findElements(By.tagName("input"));
// just print the value of all textboxes
for (WebElement e: allInputs) {
if (e.getAttribute(“type”).equals(“text”)){
System.out.println(e.getText().toString()); //Print out the value in each text box
}
}
5:WebElement searchBox = driver.findElement(By.className("buttonStyle"));
6:WebElement aboutLink = driver.findElement(By.linkText("About Google"));
7: WebElement aboutLink = driver.findElement(By.partialLinkText("About"));
8: The following is a reference to the relative path:
Find the page root element: //
Find all input elements on the page: //input
Find the direct child input element within the first form element on the page (that is, only include the next level input element of the form element, represented by an absolute path, single/number): //form[1]/input
Find all child input elements within the first form element on the page (as long as the input within the form element counts, no matter how many other tags are nested, use relative paths, double // signs): //form[ 1]//input
Find the first form element on the page: //form[1]
Find the form element with the id loginForm on the page: //form[@id='loginForm']
Find input elements with name attribute username on the page: //input[@name='username']
Find the first input element under the form element with the id of loginForm on the page: //form[@id='loginForm']/input[1]
Find pages with input elements whose name attribute is continue and type attribute is button: //input[@name='continue'][@type='button']
Find the fourth input element under the form element whose id is loginForm on the page: //form[@id='loginForm']/input[4]
1 driver.findElement(By.xpath(“//a[contains(@href, ‘logout’)]”));
1 driver.findElement(By.xpath(“//a[starts-with(@rel, ‘nofo’)]));
1 driver.findElement(By.xpath(“//*[text()=’退出’]));
1 driver.findElement(By.xpath(“//a[contains(text(), ’退出’)]));
8:
WebElement password = driver.findElement(By.cssSelector("#J_login_form>dl>dt>input[id=’ J_password’]"));
Another use of cssSelector is to locate elements that use composite style sheets, as mentioned in the fourth method className. Now let's take a look at how to refer to the button mentioned in the fourth method through cssSelector. The button code is as follows:
<button id="J_sidebar_login" class="btn btn_big btn_submit" type="submit">登录</button>
The cssSelector reference element code is as follows:
driver.findElement(By.cssSelector("button.btn.btn_big.btn_submit"))